非常简单的代码,不工作

时间:2017-12-05 14:47:29

标签: python python-3.x

我试图根据用户输入引出两个响应,但无法使其工作。它只是保持打印"Correct, seems you're smarter than I thought..."。非常感谢任何帮助,谢谢

print ("Welcome to UTOPIA")


river= ""
while not river:
    river = input ("\n\n\n\nYou come across a raging river, what do you do? ")

if river == "swim" or "swim through it":
    print ("Correct, seems you're smarter than I thought...")

elif river == "walk through it" or "walk through":
    print ("You cant walk through such a big river... silly!")

else:
    print ("Well, sensible suggestions now...")

3 个答案:

答案 0 :(得分:2)

问题与你的if语句有关。是或不会自动查看最后使用的变量,因此必须再次指定。如果字符串不为空,“some string”将始终评估为true。

print ("Welcome to UTOPIA")

river= ""
while not river:
    river = input ("\n\n\n\nYou come across a raging river, what do you do? ")

if river == "swim" or river == "swim through it": #Notice the change
    print ("Correct, seems you're smarter than I thought...")

elif river == "walk through it" or river == "walk through": #Notice the change
    print ("You cant walk through such a big river... silly!")

else:
    print ("Well, sensible suggestions now...")

答案 1 :(得分:0)

因为你正在做

if river == "swim" or "swim through it":
    # ...

这是不正确的代码。 基本上你说的是“如果河是游泳或弦”游过它“”,这是没有意义的。

您正在寻找

if river == "swim" or river == "swim through it"

答案 2 :(得分:0)

Python中的所有非空字符串(不幸的是)True

这意味着这将永远是True

if something or "string":

因为or "string"始终为True