簡體   English   中英

為什么這個 if-else 語句不起作用?

[英]Why is this if-else statement not working?

所以,如果這個人回答“nutella”,它會打印“那是正確的!” 我如何才能說“你錯了!” 如果他們回答其他任何問題。

nutella = input

input("What's the best food in the world?")

if nutella:

    print("That's correct!")

else: 

    print("You're wrong!")

只要字符串不為空,Python 中用於字符串的if語句就為真。 您想要的是將nutella變量與預期的字符串進行比較。 如:

nutella = input("What's the best food in the world?")

if nutella == "nutella":
    print("That's correct!")
else: 
    print("You're wrong!")

首先,您似乎應該查看if語句,因為您還不知道如何使用它們,但這就是您的操作方式,我也會嘗試解釋一下

choice = input("What's the best food in the world?")
if choice == "Nutella":
    print("That's correct!")
else:
    print("You're wrong!")

因此,第一行將人輸入的內容分配給變量choice ,然后在下一行檢查變量的值是否等於/是"nutella" ,如果是,它將打印"That's correct!" ,如果不是,它會打印"You're wrong!" .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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