I am creating a quiz program that draws a question and answer from a .txt file. The questions and answers are in the format:
question,answer
in the .txt file. The program picks one at random, splits it at the comma and asks the question. It then matches the users answer to the questions answer and checks if it is correct like this:
if userAnswer == question[1]:
print('Correct!')
else:
print('Incorrect, better luck next time!')
Every time I run the program I get no errors but no matter what I put, the program tells me I'm incorrect. I've asked a few friends but they are all as confused as me and none of us have any clue what the problem is. The code the splits the question and answer:
f = open('Music.txt', 'r')
question = random.choice(list(f))
question = question.split(',')
userAnswer = input(f'Who sang {question[0]}? ').lower()
I should clarify, the .txt file questions are all songs and all the answers are bands/singers. None of the answers contain comma's.
I guess that since you questions,answers are new line separated, the answer from you read from the file ends with '\n'. In order to remove it, run: answer = answer.rstrip()
and then compare it to the answer provided as the user input.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments