Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:03:25+00:00 2026-06-11T15:03:25+00:00

I am using Python 2.6, and am having difficulty understanding why the following code

  • 0

I am using Python 2.6, and am having difficulty understanding why the following code is throwing an IndexError at the location it is being thrown. The error occurs (incredibly rarely) when this version of the Porter Stemmer is incorporated into a web service.

The code involves a series of “if-elif-elif-else” statements which check an index of the input word for a series of scenarios. Note that self.k is an integer value (a placeholder) and self.ends(val) returns either a 0 or 1.

if self.b[self.k - 1] == 'a':
    if self.ends("al"): pass
    else: return
elif self.b[self.k - 1] == 'c':
    if self.ends("ance"): pass
    elif self.ends("ence"): pass
    else: return

...additional "elifs" appear here, but none modify self.b or self.k ...

elif self.b[self.k - 1] == 's':
    if self.ends("ism"): pass
    else: return

But, rarely (the input is highly variable), one of the “elif” statements throws an IndexError. For example:

line 290, in step4
elif self.b[self.k - 1] == 's':
IndexError: string index out of range

What I cannot understand is why the evaluation of an “elif” is throwing an IndexError rather than the initial “if” statement? I do not yet have data on what input is throwing the error (again, the occurrence is very rare). It’s also possible that the stemmer is receiving some type of “bad” input… Is there anything that I am missing/should be aware of with respect to Python if-elifs? (I am aware that an “elif” cannot precede “if”…).

Thanks, and let me know if I can provide any additional information.

Also, if you’re interested in (most of) the full code, I’m using a modified version of this: http://tartarus.org/~martin/PorterStemmer/python.txt, but I don’t think this is relevant to my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:03:26+00:00Added an answer on June 11, 2026 at 3:03 pm

    If nobody else is modifying self.b or self.k, what’s the point of checking it over and over again? Store it in a variable before the first if, and use it:

    c = self.b[self.k - 1]
    if c=='a': ...
    elif c=='c': ...
    

    Now, if someone else is modifying self.b or self.k in another thread, you should store it in a variable before your first if, and use it:

    c = self.b[self.k - 1]
    if c=='a': ...
    elif c=='c': ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulty downloading the Android source code using the recommended instructions, due
I'm a programming newbie having difficulty with Python multiplication. I have code like this:
I'm running opencv 2.4.1 using python bindings and am having difficulty calculating the optical
I am having difficulty in formatting some code in Python: My code is here:
I'm using Boost.Python to expose my C++ code to Python. I've encountered a difficulty
I'm having a difficulty formatting a table in html through python. I'm using for-loops
I'm having trouble using python to write strings into a file: (what I'm trying
I am using Python 2.7 lxml and I am having problems finding the table.
I'm having trouble creating an instance of a class using Python's re module. Here's
I'm having trouble with encodings when calling the Quickbase API using Python. I call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.