I am a complete novice with Python and working on a multiple choice quiz that reads questions from a file and keeps a score that then writes to a file.
Everything was working perfectly until I added validation to the answers given by the user. Now when I run the program it says my answers are incorrect!
What have I done?
Version 1 that works
def inputandoutput():questions_file = open_file("questions.txt", "r")title = next_line(questions_file)welcome(title)score = 0# get first blockcategory, question, answers, correct, explanation = next_block(questions_file)while category:# ask a questionprint(category)print(question)for i in range(4):print("\t", i + 1, "-", answers[i])# get answeranswer = input("What's your answer?: ")# check answerif answer == correct:print("\nRight!", end=" ")score += 1else:print("\nWrong.", end=" ")print(explanation)print("Score:", score, "\n\n")# get next blockcategory, question, answers, correct, explanation = next_block(questions_file)questions_file.close()
version 2 that says I now have the wrong answers
def inputandoutput():questions_file = open_file("questions.txt", "r")title = next_line(questions_file)welcome(title)score = 0# get first blockcategory, question, answers, correct, explanation = next_block(questions_file)while category:# ask a questionprint(category)print(question)for i in range(4):print("\t", i + 1, "-", answers[i])# get answer and validatewhile True:try:answer = int(input("What's your answer?: "))if answer in range (1,5):breakexcept ValueError:print ("That's not a number")else:print ("the number needs to be between 1 and 4, try again ")# check answerif answer == correct:print("\nRight!", end=" ")score += 1else:print("\nWrong.", end=" ")print(explanation)print("Score:", score, "\n\n")# get next blockcategory, question, answers, correct, explanation = next_block(questions_file)
Help?