[英]'if a == b[0]' not working when using readlines() SOLVED
我正在编写一个谁想成为百万富翁程序,但我遇到的问题是,即使用户输入了正确的答案,它也会说这是错误的答案。 我对从文件中读取整个内容是新手,所以我可能缺少一些东西。
示例 output:
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]):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.