无论您输入什么,仅显示用于输入yes的消息

时间:2017-06-20 00:58:34

标签: python-3.x

当我运行此代码时,无论我输入什么,它总是出现“我同意!”。 任何帮助表示赞赏!

Pizza_opinion = input("Does pizza taste good? ")

if Pizza_opinion == "Yes" or "yes":
    print("I agree! ")
elif Pizza_opinion == "No" or "no":
    print ("I disagree...?")

2 个答案:

答案 0 :(得分:1)

您没有正确使用条件 - 您不能使用or这样的(您可以,但它不能做您的意思)。你基本上告诉Python if Pizza_opinion equals "Yes", or if "yes" evaluates to True (and it does) then print "I agree "。使用:

if Pizza_opinion == "Yes" or Pizza_opinion == "yes":
    print("I agree! ")
elif Pizza_opinion == "No" or Pizza_opinion == "no":
    print ("I disagree...?")

或者:

if Pizza_opinion in ("Yes", "yes"):
    print("I agree! ")
elif Pizza_opinion in ("No", "no"):
    print ("I disagree...?")

或者简单地说:

if Pizza_opinion.lower() == "yes":
    print("I agree! ")
elif Pizza_opinion.lower() == "no":
    print ("I disagree...?")

答案 1 :(得分:1)

您正在考虑条件检查,Pizza_opinion等于"是"或等于"是"但它真正做的是检查Pizza_opinion是否等于"是"或者如果字符串文字"是"这是真的,它始终如此。 与elif条件相同的问题。

解决方案是重复使用"是":

进行的等式检查
if Pizza_opinion == "Yes" or Pizza_opinion == "yes":