Python 问题中的“或”条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17375793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:02:24  来源:igfitidea点击:

"or" conditional in Python troubles

pythonconditional-statementsconditional-operator

提问by Interrupt

I'm learning Python and I'm having a little bit of a problem. Came up with this short script after seeing something similar in a course I'm taking. I've used "or" with "if" before with success (it doesn't show much here). For some reason I can't seem to get this working:

我正在学习 Python,但遇到了一些问题。在我正在参加的课程中看到类似的内容后,想出了这个简短的脚本。我之前成功使用过“or”和“if”(这里没有显示太多)。出于某种原因,我似乎无法正常工作:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey" or "monkeys":
    print "You're right, they are awesome!!"
elif x != "monkey" or "monkeys":
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."

But this works great:

但这很好用:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey":
    print "You're right, they are awesome!!"
elif x != "monkey":
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."

Probably the or conditional does not fit here. But I've tried and, etc. I'd love a way to make this accept monkey or monkeys and everything else triggers the elif.

可能 or 条件不适合这里。但我已经尝试过,等等。我很想有一种方法让这个接受猴子或猴子,而其他一切都会触发 elif。

采纳答案by Barmar

Boolean expressions in most programming languages don't follow the same grammar rules as English. You have to do separate comparisons with each string, and connect them with or:

大多数编程语言中的布尔表达式不遵循与英语相同的语法规则。您必须对每个字符串进行单独比较,并将它们连接到or

if x == "monkey" or x == "monkeys":
    print "You're right, they are awesome!!"
else:
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."

You don't need to do the test for the incorrect case, just use else. But if you did, it would be:

您不需要对不正确的情况进行测试,只需使用else. 但如果你这样做了,那就是:

elif x != "monkey" and x != "monkeys"

Do you remember learning about deMorgan's Lawsin logic class? They explain how to invert a conjunction or disjunction.

你还记得在逻辑课上学习过德摩根定律吗?他们解释了如何反转连接或分离。

回答by gkayling

Should be if x == "monkey" or x == "monkeys":

应该 if x == "monkey" or x == "monkeys":

回答by Rob Falck

gkayling is correct. Your first if statement returns true if:

gkayling 是正确的。您的第一个 if 语句在以下情况下返回 true:

x == "monkey"

x ==“猴子”

or

或者

"monkeys" evaluates to true (it does since it's not a null string).

"monkeys" 的计算结果为 true(它确实如此,因为它不是空字符串)。

When you want to test if x is one of several values, it's convenient to use the "in" operator:

当您想测试 x 是否为多个值之一时,使用“in”运算符很方便:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x in ["monkey","monkeys"]:
    print "You're right, they are awesome!!"
else:
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right