简体   繁体   English

索引出错

[英]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. 字符串的索引从0到length-1。 So change the while loop condition in line 24 to: 因此,将第24行的while循环条件更改为:

while q < len(userWordEval):

Your problem is that you are looping while q <= len(userWordEval) . 您的问题是您在q <= len(userWordEval)循环。 First of all, it is important to know that in python lists use zero-based indexing (see description on Wikipedia ). 首先,重要的是要知道在python列表中使用基于零的索引(请参阅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. 这意味着如果列表中有5个元素,则最后一个元素将具有索引4。函数len返回列表中元素的数量,因此,如果将该数字用作索引,它将太大。 You can easily fix this by changing to q < len(userWordEval) . 您可以通过更改为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. 请记住,如果您尝试访问超出索引的元素,则列表,字符串,元组或其他支持索引的类型将引发IndexError。

>>> 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 因此请务必使用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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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