卡在列表更新:列表索引必须是intiger

时间:2018-05-28 16:18:39

标签: python python-2.7

到目前为止,我已经编写了这段代码,但我仍然坚持更新列表。我尝试了不同的方法,也用for循环,但我无法解决它。

print "hello, welcome to"
a_list=["1. Add a new word","2. Update and existing word","3. Detele and existing word","4. Display a words definition"]
zero=0
empt_list=[]
empt_list_meaning=[]
def list_game():
    for i in a_list:
        print i
    a_options=input("Please select one of these options: ")
    if a_options== 1:
        a_newword=raw_input("What word you want to add? ")
        empt_list.append(a_newword)
        a_newword_meaning=raw_input("add the meaning of the word")
        empt_list_meaning.append(a_newword_meaning)
        # print a_list[a_options-1]
        print empt_list,a_newword,"added correctly"

    elif a_options == 2:
        a_update=raw_input("select a word to update:")
        a_renew_word = raw_input("the new word")
        if a_update in empt_list:
            empt_list[a_update]=a_renew_word
        else:
            print "sorry"
        # print a_list[a_options-1]

    elif a_options == 3:
        a_del_word=raw_input("selct the word you want to delete")
        for i in empt_list:
            if a_del_word in empt_list:
                empt_list.remove(a_del_word)
        # print a_list [a_options-1]


    elif a_options  == 4:
        for i in empt_list:
            print i
    print ("would you like to continue or exit?\n1.contine\n2.exit")
    now=input(">>> ")
    if now==1:
        list_game()
    else:
        print "arrivederchi"
list_game()

这是追溯:

Traceback (most recent call last):
  File "C:/Users/Umer Selmani/Desktop/newproject2018.py", line 44, in <module>
    list_game()
  File "C:/Users/Umer Selmani/Desktop/newproject2018.py", line 41, in list_game
    list_game()
  File "C:/Users/Umer Selmani/Desktop/newproject2018.py", line 22, in list_game
    empt_list[a_update]=a_renew_word
TypeError: list indices must be integers, not str

Process finished with exit code 1

2 个答案:

答案 0 :(得分:0)

我认为是这一点造成了麻烦。 a_update是一个字符串,而不是整数。列表索引必须是整数。请尝试以下更新的代码:

elif a_options == 2:
    a_update=raw_input("select a word to update:")
    # Note a_update is a string that user enters
    a_renew_word = raw_input("the new word")
    if a_update in empt_list:
        # This bit is changed from empt_list[a_update] to...
        empt_list.index(a_update)=a_renew_word
    else:
        print "sorry"
    # print a_list[a_options-1]

答案 1 :(得分:0)

你得到的这部分代码的错误,

elif a_options == 2:
        a_update=raw_input("select a word to update:")
        a_renew_word = raw_input("the new word")
        if a_update in empt_list:
            empt_list[a_update]=a_renew_word
        else:
            print "sorry"
        # print a_list[a_options-1]

由于列表索引必须是整数,因此您应该在下面的代码中将整数作为列表索引传递。以下是另一种方法,

if a_update in empt_list:
        #updated code
        lst_index=empt_list.index(a_update)
        empt_list[lst_index]=a_renew_word
    else:
        print "sorry"

如果empt_list是字典而不是列表,那么你提到的方式可能有用。由于字典以键,值对的形式存储数据,因此它们可以将整数和字符串作为键。