簡體   English   中英

'if a == b[0]' 在使用 readlines() 時不起作用已解決

[英]'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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM