I am writing a spanish quiz in python, and I am running into a problem when the user inputs an incorrect answer in the quiz.
import random
def create_dictionary(filename):
dictionary = {}
file = open(filename)
for line in file:
line = line.replace('\n','')
split = line.split(':')
spanish_words = split[1].split(',')
dictionary[split[0]] = spanish_words
file.close()
return dictionary
def main():
dictionary = create_dictionary('project13_data.txt')
print (dictionary)
questions = int(input("How many questions do you want to be quizzed on? "))
final = questions
wrong = []
while questions > 0:
def good_guess():
if val == answer[0] or answer[1]:
print("Correct\n")
else:
print("Wrong\n")
wrong.append(find)
find = random.choice(list(dictionary.keys()))
answer = dictionary[find][0:2]
print(find)
print(answer)
print("What is" ,find, "in spanish? ")
val = input("Answer: ")
good_guess()
questions = questions - 1
print("You got", len(wrong), "wrong out of", final)
print(list(wrong))
main()
The error that i am getting is
File "C:\Python34\Project 13 take 2.py", line 33, in good_guess
if val == answer[0] or answer[1]:
IndexError: list index out of range
If the user inputs a correct answer, the code runs fine, but otherwise I get the error. I do not know why I am getting this error, what can I do to fix this?