简体   繁体   English

我该如何解决这个列表问题(列表索引超出范围)

[英]How can I fix this list problem (list index out of range)

I'm new to programming and trying to make a vocabulary test machine in python, and the problem I'm having is when you're restarting the test and trying to redo the wrong answers, the correct answers being inputted should be removed from the list which contains the wrong answers.我是编程新手,正在尝试用 python 制作词汇测试机,我遇到的问题是当您重新开始测试并尝试重做错误答案时,输入的正确答案应该从包含错误答案的列表。

So basically, in beginning you are putting in custom vocabulary words and then answering them, if any words are incorrect they are put in a list, and when redoing the test, if they are correct this time they should be removed from the list.所以基本上,一开始你输入自定义词汇然后回答它们,如果有任何单词不正确,它们会被放入一个列表中,当重做测试时,如果这次它们是正确的,它们应该从列表中删除。 So at the end if you are answering the words correctly the list should be empty.所以最后,如果你正确回答了单词,那么列表应该是空的。 But I am getting this error: IndexError: list index out of range但我收到此错误:IndexError:列表索引超出范围

What do you suggest I should do?你建议我应该怎么做?

(I translated the code from another language so if anything is not matching that's the reason) (我从另一种语言翻译了代码,所以如果有什么不匹配的,那就是原因)

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()
 

As I can't comment yet: I think the problem is that this因为我还不能发表评论:我认为问题是这个

for x in range(len(wordwrong)):

loop is trying to go through all of the elements of wordwrong in loop 试图遍历wordwrong in 中的所有元素

wordwrong.remove(wordwrong[x])

even though the size of wordwrong is changing and getting smaller each time a word is removed from the list.即使每次从列表中删除一个单词时, wordwrong的大小都会发生变化并变小。 I might be wrong though.不过我可能是错的。 I'll hope this helps you.我希望这对你有帮助。

One immediate suggestion is that you're inputting Swedish words and their English equivalent, and then storing these in separate parallel lists.一个直接的建议是您输入瑞典语单词及其对应的英语单词,然后将它们存储在单独的并行列表中。 This is not wrong, per se, but frequently troublesome.这本身并没有错,但常常很麻烦。 You could store them as a list of tuples, or even better as a dictionary.您可以将它们存储为元组列表,或者更好地存储为字典。

words = {}

while True:
  print("Type: 'Stop' to stop")
  swedish = input("Enter Swedish word: ")
  if swedish.lower() == "stop": break
  english = input("Enter English equivalent: ")
  words[swedish] = english

Now, if you want to iterate over all of the Swedish words and their English equivalents:现在,如果您想遍历所有瑞典语单词及其对应的英语单词:

for swedish, english in words.items():
  ...

If the user gets two attempts for each translation, and you want to track how many they translate correctly:如果用户对每个翻译进行两次尝试,并且您想跟踪他们正确翻译的次数:

correct = 0
attempts = 2

for swedish, english in words.items():
  correct_translation = False  

  for attempt in range(attempts):
    translation = input(f"Translate Swedish word {swedish} to English: ")

    if translation.lower() == english.lower():
      correct_translation = True
      break
    elif attempt == attempts - 1:
      print("Incorrect.")
    else:
      print("Incorrect. Try again.")

  if correct_translation: correct += 1

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

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