I am making a little program in python that is supposed to read the questions from a text file in the format of give question, answers ABCD and should check if the user put in the correct answer. Below is the code I wrote for doing that
import time
file = open("Questions.txt", "r")
def askQuestion():
print(file.readline())
for counter in range(4):
print(file.readline())
x = file.readline()
userAnswer = input("Please input a letter that corresponds to the correct answer")
print("The answer is", userAnswer)
print("The X is", x)
if userAnswer.upper() == x:
print("You got that right")
for counter in range(10):
time.sleep(1)
askQuestion()
file.close()
and here is a text file with the questions and answers, in advance I just want to say that I am not sure if this is how I should format the text in the file so I am sorry if it isn't the right way to do it on stack overflow.
1) What was the name of the atomic bomb dropped on Hiroshima in 1945?
A)Fat Man
B)Little Boy
C)Annihilator
D)Heisenberg
B
2)How many stars is there on the American Flag?
A)48
B)47
C)50
D)52
C
3)How many countries is there in Europe?
A)52
B)38
C)12
D)28
D
4)What is the capital city of Poland?
A)Warsaw
B)Krakow
C)Kijew
D)Moscow
A
5)What are the colors on the polish flag?
A)RED-WHITE
B)WHITE-RED
C)WHITE-GREEN
D)YELLOW-BLUE
B
6)What does 2+2*2 evaluate to?
A)8
B)10
C)6
D)20
C
7)What year do we have?
A)3920
B)120
C)20018
D)2018
D
8)When did WW2 end?
A)1945
B)1919
C)1905
D)1834
A
9)When was Python 3 realesed?
A)2000
B)2012
C)2010
D)2014
C
10)Who is the president of USA?
A)Micheele Obama
B)Barack Obama
C)George Washington
D)Donald Trump
D
My problem is that let's say for the first question the answer is "B" which is saved in variable x (to make sure x was actually "B" I printed it as seen in the code. Then I printed the user input which also was "B", but for some reason python does not execute the if statement below, even though the condition seems to be True. The condition states that userAnswer (which is where the user input is stored) is equal to the variable x the statement "You got that right should print" this is however not happening and the statement appears to evaluate as false, because if I put an else statement below it, it evaluates the else statement to true and executes the code under it. I would be really thankful if anyone could help me figure this one out.
Aucun commentaire:
Enregistrer un commentaire