Home > Enterprise >  How can I fix this list problem (list index out of range)
How can I fix this list problem (list index out of range)

Time:12-14

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.

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

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

CodePudding user response:

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

wordwrong.remove(wordwrong[x])

even though the size of wordwrong is changing and getting smaller each time a word is removed from the list. I might be wrong though. I'll hope this helps you.

CodePudding user response:

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
  • Related