I am making a true or false quiz for my computer science class, and I have to import data from a .txt file. When prompted to answer a question, you must type ‘TRUE’ or ‘FALSE’. My code does not see these inputs as being the same as the TRUE and FALSE in the .txt file, so no answer can be correct.
Here is the .txt file
And here is my code:
import random
def makeChoice():
invalid = True
while invalid == True:
x = input("Is this TRUE or FALSE? ")
print("")
if x == 'TRUE' or x == 'FALSE':
invalid = False
else:
print("Invalid Choice - Please Type 'TRUE' or 'FALSE'n")
else:
return (x)
class Question:
def __init__(self, text, answer):
self.text = text
self.answer = str(answer)
def __str__(self):
return f'{self.text}{self.answer}'
class Game:
def __init__(self):
self.list = []
self.score = 0
def intro(self):
print("This is a 10 question true or false test.")
print("nWhen entering your answer, please type 'TRUE' or 'FALSE' in ALL CAPS")
print("nGood luck!")
def make_list(self):
file = open("true_false.txt", "r")
q1 = Question(file.readline(), file.readline())
q2 = Question(file.readline(), file.readline())
q3 = Question(file.readline(), file.readline())
q4 = Question(file.readline(), file.readline())
q5 = Question(file.readline(), file.readline())
q6 = Question(file.readline(), file.readline())
q7 = Question(file.readline(), file.readline())
q8 = Question(file.readline(), file.readline())
q9 = Question(file.readline(), file.readline())
q10 = Question(file.readline(), file.readline())
self.list = [q1, q2, q3, q4, q5, q6, q7, q8, q9, q10]
file.close()
random.shuffle(self.list)
def ask_question(self):
x = self.list.pop()
print(x.text)
print(str(x.answer))
answer = makeChoice()
if answer == "FALSE":
print("Answer recieved: FALSE")
elif answer == "TRUE":
print("Answer received: TRUE")
else:
print("ERROR")
if answer == "FALSE" and x.answer == "FALSE":
print("nCorrect! The answer was", x.answer)
self.score = self.score + 1
elif answer == "TRUE" and x.answer == "TRUE":
print("nCorrect! The answer was", x.answer)
self.score = self.score + 1
else:
print("nIncorrect! The answer was", x.answer)
if x.answer == "FALSE" or x.answer == "TRUE":
print("This should work")
else:
print("This is impossible")
x = 0
def end_game(self):
print("nYou got ", self.score, " out of 10 questions correct!")
game = Game()
game.intro()
game.make_list()
for i in range(9):
game.ask_question()
game.end_game()
Any help is greatly appreciated!!
I try to enter ‘TRUE’ and get the correct answer, but it does not see TRUE == TRUE and vice versa for false.
RealmXD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.