索引错误有问题

时间:2015-10-05 03:52:24

标签: python

我正在为我的第一个计算机编程课程做作业,我遇到了一个问题。基本上这个程序应该用一个发音的夏威夷语,并产生一个字符串,显示如何撬它。但是当我运行程序时,会发生这种情况:

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

输出:

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

3 个答案:

答案 0 :(得分:1)

string的索引是从0到length-1。因此,将第24行中的while循环条件更改为:

while q < len(userWordEval):

答案 1 :(得分:0)

您的问题是您在q <= len(userWordEval)时正在循环播放。首先,重要的是要知道在python列表中使用从零开始的索引(参见description on Wikipedia)。这意味着如果列表中有5个元素,则最后一个元素将具有索引4.函数len返回列表中元素的数量,因此如果您将该数字用作索引,则它将太大。您可以通过更改为q < len(userWordEval)轻松解决此问题。

答案 2 :(得分:0)

如果您尝试访问索引之后的元素,请记住列表,字符串,元组或其他支持索引的类型将引发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

所以总是使用len(s)-1

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

这里有一点好事。但是在切片期间,您不会收到该错误。它将简单地返回一个空字符串/列表。

>>> a[5:]
''
>>> a[:11]
'apple'
相关问题