列表更新报错:list indices must be integers, not str 求助
解决单词更新操作中的TypeError问题
先看你遇到的核心报错:
TypeError: list indices must be integers, not str
这个错误出现在第22行的empt_list[a_update]=a_renew_word,原因很直白:列表的索引必须是整数(元素的位置序号),但你这里用了字符串类型的单词a_update当索引,Python肯定识别不了呀!
问题根源分析
你现在用两个独立的列表empt_list和empt_list_meaning分别存单词和释义,这种拆分存储的方式在需要关联修改、查找时会非常麻烦。比如要更新某个单词,你得先找到它在empt_list里的位置,再修改对应元素,还要同步调整empt_list_meaning的对应位置,很容易出错。
最优修复方案
对于单词和释义这种一一对应的场景,字典是最适合的数据结构——用单词做键,释义做值,增删改查都能一步到位。我帮你重构了代码,同时修复了拼写错误(比如"Detele"→"Delete"、"selct"→"select"),还增加了输入验证:
print("hello, welcome to Word Manager") # 用字典存储单词与释义的键值对,替代两个独立列表 word_dict = {} def list_game(): print("\nAvailable Options:") a_list = [ "1. Add a new word", "2. Update an existing word", "3. Delete an existing word", "4. Display all words and their definitions" ] for option in a_list: print(option) # 处理输入,防止非数字输入报错 try: a_options = int(input("Please select one of these options: ")) except ValueError: print("Please enter a valid number between 1-4!") list_game() return if a_options == 1: new_word = raw_input("What word do you want to add? ").strip() if new_word in word_dict: print("This word already exists in the manager!") else: new_meaning = raw_input("Add the meaning of the word: ").strip() word_dict[new_word] = new_meaning print(f"{new_word} added correctly!") elif a_options == 2: target_word = raw_input("Select a word to update: ").strip() if target_word in word_dict: updated_word = raw_input("Enter the new word: ").strip() updated_meaning = raw_input("Enter the meaning of the new word: ").strip() # 删除旧单词,添加新的键值对 del word_dict[target_word] word_dict[updated_word] = updated_meaning print(f"Successfully updated! '{target_word}' replaced with '{updated_word}'") else: print("Sorry, that word doesn't exist in the list!") elif a_options == 3: del_word = raw_input("Select the word you want to delete: ").strip() if del_word in word_dict: del word_dict[del_word] print(f"{del_word} deleted successfully!") else: print("Sorry, that word isn't in the list!") elif a_options == 4: if not word_dict: print("No words stored in the manager yet!") else: print("\nWords and their definitions:") for word, meaning in word_dict.items(): print(f"- {word}: {meaning}") else: print("Invalid option! Please choose a number between 1-4.") # 询问是否继续操作 print("\nWould you like to continue or exit?") print("1. Continue") print("2. Exit") try: choice = int(input(">>> ")) if choice == 1: list_game() elif choice == 2: print("arrivederchi!") else: print("Invalid choice, exiting...") except ValueError: print("Invalid input, exiting...") list_game()
关键改进点
- 用
word_dict字典替代两个独立列表,直接通过单词(键)操作对应的释义(值),彻底解决索引错误问题 - 增加输入验证逻辑,避免用户输入非数字导致程序崩溃
- 优化提示信息的可读性,处理了单词已存在、列表为空等边界情况
- 修复了原代码中的拼写错误
如果你坚持要使用列表的方式,那更新时需要先找到单词的索引位置:
elif a_options == 2: a_update = raw_input("select a word to update:").strip() if a_update in empt_list: # 找到目标单词的索引 idx = empt_list.index(a_update) a_renew_word = raw_input("the new word").strip() empt_list[idx] = a_renew_word # 同步更新释义列表的对应位置 a_renew_meaning = raw_input("the new meaning").strip() empt_list_meaning[idx] = a_renew_meaning print("Updated successfully!") else: print("sorry")
不过这种方式不如字典高效,当单词数量较多时,很容易出现数据不同步的问题。
内容的提问来源于stack exchange,提问作者user9575606
相关产品推荐
相关产品推荐

