简体   繁体   中英

Having trouble with an index Error

I am doing an assignment for my first computer programming course, and I am running into a problem. Basically this program is supposed to take a phonetically Hawaiian word, and produce a string that shows how to prounce it. However when I run the program, this happens:

stopProgram = 1

while stopProgram == 1:

    validWord = 0

    while validWord == 0: 
    #this while loop is has the user enter a word until it means Hawaiian syntax.
        userWord = input("Please enter a valid hawaiian word.")
        userWordEval = userWord.lower()
        #changed the case for easier comparisons
        validInput = 0
        for j in range (len(userWordEval)):
        #Test every character in the word to see if it meets the requirements. If it does, valid word is added 1.
            if userWordEval[j] == "a" or userWordEval[j] == "e" or userWordEval[j] == "i" or userWordEval[j] == "o" or userWordEval[j] == "u" or userWordEval[j] == "p" or userWordEval[j] == "k" or userWordEval[j] == "h" or userWordEval[j] == "l" or userWordEval[j] == "m" or userWordEval[j] == "n" or userWordEval[j] == "w" or userWordEval[j] == "'" or userWordEval[j] == " ":
                validInput += 1    

        if validInput == len(userWordEval):
        #if the number in validWord is equal to the length of the word the user put in, that means that all the charaters were accepted. Otherwise, that means that something wasn't allowed, and will have to be reentered.
            validWord = 1
        else:
            print("Invalid input. The accepted characters are: a, e, i, o, u, p, k, h, l, m, n, w, and '")

    proWord = "" #Using this for the pronunciation string.

    q = 0

    while q <= len(userWordEval):

        if userWordEval[q] == "a":
            if len(userWordEval[q:]) > 1:
                if userWordEval[q+1] == "i":
                    proWord += "-eye"
                    q += 2
                elif userWordEval[q+1] == "e":
                    proWord += "-eye"
                    q += 2
                elif userWordEval[q+1] == "o":
                    proWord += "-ow"
                    q += 2
                elif userWordEval[q+1] == "u":
                    proWord += "-ow"
                    q += 2
                elif userWordEval[q+1] == "'":
                    proWord += "-ah"
                    q += 2
                else:
                    proWord += "-ah"
                    q += 1
            else:
                proWord += "-ah"
                q += 1

        elif userWordEval[q] == "e":
            if len(userWordEval[q:]) > 1:
                if userWordEval[q+1] == "i":
                    proWord += "-ay"
                    q += 2
                elif userWordEval[q+1] == "u":
                    proWord += "-ow"
                    q += 2
                elif userWordEval[q+1] == "'":
                    proWord += "-eh"
                    q += 2
                else:
                    proWord += "-eh"
                    q += 1
            else:
                proWord += "-eh"
                q += 1

        elif userWordEval[q] == "i":
            if len(userWordEval[q:]) > 1:
                if userWordEval[q+1] == "u":
                    proWord += "-ay"
                    q += 2
                elif userWordEval[q+1] == "'":
                    proWord += "-ee"
                    q += 2
                else:
                    proWord += "-ee"
                    q += 1
            else:
                proWord += "-ee"
                q += 1

        elif userWordEval[q] == "o":
            if len(userWordEval[q:]) > 1:
                if userWordEval[q+1] == "i":
                    proWord += "-oy"
                    q += 2
                elif userWordEval[q+1] == "u":
                    proWord += "-ow"
                    q += 2
                elif userWordEval[q+1] == "'":
                    proWord += "-oh"
                    q += 2
                else:
                    proWord += "-oh"
                    q += 1
            else:
                proWord += "-oh"
                q += 1

        elif userWordEval[q] == "u":
            if len(userWordEval[q:]) > 1:
                if userWordEval[q+1] == "i":
                    proWord += "-ooey"
                    q += 2
                elif userWordEval[q+1] == "'":
                    proWord += "-oo"
                    q += 2
                else:
                    proWord += "-oo"
                    q += 1
            else:
                proWord += "-oo"
                q += 1
        else:
            q + 1

    print(proWord)
    stopProgram = 0

Output:

Please enter a valid hawaiian word.aeae Traceback (most recent call last):
   File "C:/Users/Kristopher/Documents/Programming HW/Program
   3.py", line 26, in <module>
   if userWordEval[q] == "a": IndexError: string index out of range

string's index is from 0 to length-1. So change the while loop condition in line 24 to:

while q < len(userWordEval):

Your problem is that you are looping while q <= len(userWordEval) . First of all, it is important to know that in python lists use zero-based indexing (see description on Wikipedia ). This means that if there are 5 elements in a list, the last element will have index 4. The function len returns the number of elements in a list, so if you use that number as an index it will be too large. You can easily fix this by changing to q < len(userWordEval) .

Remember list, string, tuple or other types which support indexing will raise IndexError if you try to access element past the index.

>>> a = 'apple'
>>> a[0]
'a'
>>> a[4]
'e'
>>> a[5]

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    a[5]
IndexError: string index out of range

So always use len(s)-1

>>> a[len(a)-1]
'e'
>>> 

One nice bit of gotcha here. However during slicing you won't get that error. It will simple return an empty string/list.

>>> a[5:]
''
>>> a[:11]
'apple'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM