Reputation: 13
I am trying to read the question and answer from a text file (both stored on the same line). I will then split the line uising a comma delimeter, storing the values into two variables. I will display the question to the user, who will reply. I will then compare the user input to the answer from the file. A message is then displayed based on whether or not the answer is correct.
My code is as follows:
File = open("Quiz.txt", "r")
for Line in File:
Question, Answer = Line.split(',', 1)
UserAnswer = input(Question)
if(UserAnswer == Answer):
print("Correct")
else:
print("Incorrect")
File.close()
My Quiz file is as follows:
Q1) What is a Variable used for?,Storage
Q2) How many bits in a Byte?,8
Q3) How many Bytes in a Kilobyte?,1024
The file reads in and I have used print statements to see what values are stored in the variables. All appears fine, except the comparrison between the user input and the stored values returns false (Line 5). I even tried to cast the values to strings, and the comparrison still returns false.
I know there is probably something glaringly obvious that I am overlooking, and need another set of eyes. Any help appreciated.
Upvotes: 1
Views: 23
Reputation: 296
Last character on 'Answer' is new line character. Try:
UserAnswer == Answer[:-1]
'[:-1]' removes last character from string.
Upvotes: 1