我正在编写一个"谁想成为百万富翁"程序,但我遇到的问题是,即使用户输入了正确的答案,它也会说这是错误的答案。我对从文件中阅读这件事还不熟悉,所以我可能错过了一些东西。
示例输出:
What do you think? 3
That is the wrong answer!
The right answer was 3
Thanks for playing!
下面的一些代码:
questions_file = open('questions.txt', 'r')
questions = questions_file.readlines()
choices_file = open('choices.txt', 'r')
choice = choices_file.readlines()
answers_file = open('answers.txt', 'r')
answers = answers_file.readlines()
prize_file = open('prize.txt', 'r')
prize = prize_file.readlines()
print('Question 1 for',prize[0])
print(questions[0])
print('1 -',choice[0])
print('2 -',choice[1])
print('3 -',choice[2])
print('4 -',choice[3])
answ_1 = int(input('What do you think? '))
if answ_1 == answers[0]:
print('That is the correct answer!')
else:
print('That is the wrong answer!')
print('The right answer was',answers[0])
print('Thanks for playing!')
exit()
您将answ_1
设为int(int(input('What do you think? '))
(,但来自文件的输入是字符串。
要么将answ_1
简单地设置为字符串:answ_1 = input('What do you think? ')
,要么将答案设置为int:if answ_1 == int(answers[0]):