Python词汇测试程序报错IndexError: list index out of range如何修复?
词汇测试程序IndexError问题解决
我是编程新手,正在用Python开发一款词汇测试程序。程序流程为:先录入自定义的瑞典语-英语词汇对,答题时将答错的词汇存入错题列表;重测错题时,若答对则需将该词汇从错题列表中移除,最终答对所有错题后列表应为空。但重测过程中出现报错:IndexError: list index out of range,请问该如何解决?(代码由其他语言翻译而来,可能存在不匹配情况)
import replit, sys, time, os word = [] word1 = [] wordwrong = [] wordwrong1 = [] incorrect = 0 correct = 0 swedish = "" print("Type: 'Stop' to stop") while swedish.lower() != "stop": swedish = input("\nType the word in swedish:") if swedish.lower() != "stop": word.append(swedish) else: replit.clear() break english = input("\nType the word in english:") word1.append(english) replit.clear() print("Type: 'Stop' to stop") for x in range(len(word)): wordanswer = input("translate word " + "'" + word[x] + "'" + ":").lower() replit.clear() while wordanswer != word1[x]: print("Incorrect answer! try again, " + str(2-incorrect) + " tries left") wordanswer = input("translate " + "'" + word[x] + "'" + ":") incorrect = incorrect + 1 replit.clear() if incorrect == 2: replit.clear() incorrect = incorrect-2 wordwrong.append(word[x]) wordwrong1.append(word1[x]) break else: print("Correct answer!") correct = correct + 1 incorrect = incorrect*0 replit.clear() print("Your result:", correct, "/", len(word), "correct answers " +"(" + str(correct/len(word)*100)+"%)") restart = input("\nTo restart; type 'restart':").lower() correct = correct*0 incorrect = incorrect*0 restart = "restart" while restart == "restart" and len(wordwrong) > 0: for x in range(len(wordwrong)): wordanswer = input("translate word " + "'" + wordwrong[x] + "'" + ":").lower() while wordanswer != wordwrong[x]: print("Incorrect answer! try again, " + str(2-incorrect) + " tries left") wordanswer = input("translate word " + "'" + wordwrong[x] + "'" + ":") incorrect = incorrect + 1 if incorrect == 2: incorrect = incorrect-2 break else: print("Correct answer!") correct = correct + 1 incorrect = incorrect*0 wordwrong.remove(wordwrong[x]) wordwrong1.remove(wordwrong1[x]) (here i am trying to remove the words that got corrected) print("Your result:", correct, "/", len(word), "correct answers " +"(" + str(correct/len(word)*100)+"%)") restart = input("\nTo restart; type 'restart':").lower()
问题原因
报错的核心是遍历列表时直接修改列表长度:
- 重测阶段用
for x in range(len(wordwrong))循环,循环次数基于列表初始长度。 - 当执行
wordwrong.remove(wordwrong[x])时,列表长度会缩短,但循环的x仍按初始长度递增,当x超过修改后的列表最大索引时,就会触发IndexError。 - 另外还存在逻辑错误:重测时判断答案的条件写成了
wordanswer != wordwrong[x],实际应该拿输入的英语和wordwrong1[x](对应英语翻译)对比,否则永远判断错误。
解决方案
1. 反向遍历列表
从列表最后一个元素往前遍历,这样即使前面的元素被移除,后续循环的索引不会越界。修改重测阶段的循环:
# 替换原for循环 for x in range(len(wordwrong)-1, -1, -1):
2. 修正答案判断逻辑
把重测时的while wordanswer != wordwrong[x]改成:
while wordanswer != wordwrong1[x].lower():
3. 修正其他细节
- 移除代码里的注释括号
(here i am trying to remove the words that got corrected),避免语法错误。 - 重测结束后的
restart输入缩进错误,调整到循环内。
修正后的完整代码
import replit, sys, time, os word = [] word1 = [] wordwrong = [] wordwrong1 = [] incorrect = 0 correct = 0 swedish = "" print("Type: 'Stop' to stop") while swedish.lower() != "stop": swedish = input("\nType the word in swedish:") if swedish.lower() != "stop": word.append(swedish) else: replit.clear() break english = input("\nType the word in english:") word1.append(english) replit.clear() print("Type: 'Stop' to stop") for x in range(len(word)): wordanswer = input("translate word " + "'" + word[x] + "'" + ":").lower() replit.clear() while wordanswer != word1[x].lower(): print("Incorrect answer! try again, " + str(2-incorrect) + " tries left") wordanswer = input("translate " + "'" + word[x] + "'" + ":").lower() incorrect = incorrect + 1 replit.clear() if incorrect == 2: replit.clear() incorrect = incorrect - 2 wordwrong.append(word[x]) wordwrong1.append(word1[x]) break else: print("Correct answer!") correct = correct + 1 incorrect = 0 replit.clear() print("Your result:", correct, "/", len(word), "correct answers " +"(" + str(round(correct/len(word)*100, 2))+"%)") restart = input("\nTo restart; type 'restart':").lower() correct = 0 incorrect = 0 while restart == "restart" and len(wordwrong) > 0: # 反向遍历避免索引越界 for x in range(len(wordwrong)-1, -1, -1): wordanswer = input("translate word " + "'" + wordwrong[x] + "'" + ":").lower() while wordanswer != wordwrong1[x].lower(): print("Incorrect answer! try again, " + str(2-incorrect) + " tries left") wordanswer = input("translate " + "'" + wordwrong[x] + "'" + ":").lower() incorrect = incorrect + 1 if incorrect == 2: incorrect = incorrect - 2 break else: print("Correct answer!") correct = correct + 1 incorrect = 0 # 移除答对的错题 wordwrong.pop(x) wordwrong1.pop(x) replit.clear() print("Your result:", correct, "/", len(wordwrong)+correct, "correct answers " +"(" + str(round(correct/(len(wordwrong)+correct)*100, 2))+"%)") restart = input("\nTo restart; type 'restart':").lower()
内容的提问来源于stack exchange,提问作者Maxx2774
相关产品推荐
相关产品推荐

