I am trying to make a quiz using python which reads questions from a text file. I have a variable called ans which is supposed to be the answer which it reads from the file I print the variable and it says what its supposed to say but if I actually type it in it will say its wrong.
This is my python code:
right = 0
wrong = 0
num = 0
quest = 0
history = open("history.txt", "r")
lines = history.readlines()
while quest != 3:
quest = quest+1
num = num+1
print("Question", quest)
question = lines[num]
print(question)
num = num + 1
ans = lines[num]
print(ans)
answer = input()
answer = answer.lower()
if answer == ans:
print("correct")
right = right+1
else:
print("Wrong")
wrong = wrong+0
print("done")
And my history.txt file is formatted like this
Blank Line Blank Line
What is the capital of England?
london
What is 1+1?
2
Thank you.
print(repr(ans),"=?=",repr(answer))im sure you will quickly see your issue ...chomp. I don't know how to do it in Python, or whether.readlinesorinput()do it for you, but that's probably the problem. You can easily try by printing both strings including enclosing characters to see if the final character ends up in a new line.