adding validation to answer in quiz gives wrong answers

2024/7/8 4:54:11

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?

Answer

In your original version, answer was a string; in your new version, it is an int.

If you change your try body to be:

answer = input("What's your answer?: ")
if int(answer) in range (1,5):

then you can still catch the ValueError but leave answer a string.

https://en.xdnf.cn/q/120086.html

Related Q&A

Why do I get None as the output from a print statement? [duplicate]

This question already has answers here:Why is "None" printed after my functions output?(7 answers)Closed 2 years ago.def print_name(name):print(name)print(print_name(Annabel Lee))Why do I ge…

How to collect tweets about an event that are posted on specific date using python?

I wish to collect all tweets containing specific keywords(ex:manchesterattack,manchester) that are posted about the manchester attack from 22may. Can anyone provide me a code to collect tweets using py…

Pivoting a One-Hot-Encode Dataframe

I have a pandas dataframe that looks like this:genres.head()Drama Comedy Action Crime Romance Thriller Adventure Horror Mystery Fantasy ... History Music War Documentary Sport Musical W…

How to declare multiple similar variables in python? [duplicate]

This question already has answers here:How do I create variable variables?(18 answers)Closed 5 years ago.How can I declare multiple (about 50) variables that count from slider1 to slider50 ? Is there…

what does means this error broken pipe? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:TCP client-server SIGPIPE I would like know what does this error mean?

Apply a function to each element of a pandas series

I am trying to tokenize each sentence of my pandas series. I try to do as I see in the documentation, using apply, but didnt work:x.apply(nltk.word_tokenize)If I just use nltk.word_tokenize(x) didnt wo…

ValueError: could not convert string to float: in Python 3.10

When someone writes a string or a letter, I want the code make them go back, and the code to print "must be a number and bigger than 0 and less than 100", but what actually happens is the cod…

How do I access Class fields in Python Graph-Tool property maps?

Im trying to draw a graph with a class as a vertex property. How do I draw the graph with the vertex_text set to the name field of the classes they contain?from graph_tool.all import *class Node(objec…

How to iterate through each line of a text file and get the sentiment of those lines using python?

Currently, Im working on Sentiment Analysis part. For this I have preferred to use Standford Core NLP library using python. Im able to get the sentiment for each sentence using the following code : fro…

RECURSIVE function that will sum digits of input

Trying to write a piece of code that will sum the digits of a number. Also I should add that I want the program to keep summing the digits until the sum is only 1 digit. For example, if you start with …