Python 为什么使用“OR”检查多个值的变量只检查第一个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18212574/
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 10:13:47  来源:igfitidea点击:

Why does checking a variable against multiple values with `OR` only check the first value?

python

提问by Leon Gaban

I want to check if a variable has one of multiple values. I'm confused about why ordoesn't work in this situation. I was following a tutorial that gave the example if (a or b):, but when I try to do this it only checks the variable against the first value. What is wrong with my check?

我想检查一个变量是否具有多个值之一。我很困惑为什么or在这种情况下不起作用。我正在关注给出示例的教程if (a or b):,但是当我尝试执行此操作时,它仅根据第一个值检查变量。我的支票有什么问题?

name = raw_input('Please type in your name:')

if len(name) < 5:
    print "Your name has fewer than 5 characters"
elif len(name) == 5:
    print "Your name has exactly 5 characters"
    if name == ("Jesse" or "jesse"):
        print "Hey Jesse!"
else:
    print "Your name has greater than 5 characters"

采纳答案by cdhowie

("Jesse" or "jesse")

The above expression tests whether or not "Jesse"evaluates to True. If it does, then the expression will return it; otherwise, it will return "jesse". The expression is equivalent to writing:

上面的表达式测试计算结果是否"Jesse"True。如果是,则表达式将返回它;否则,它将返回"jesse"。该表达式等效于写作:

"Jesse" if "Jesse" else "jesse"

Because "Jesse"is a non-empty string though, it will alwaysevaluate to Trueand thus be returned:

因为它"Jesse"是一个非空字符串,所以它总是计算为True并因此返回:

>>> bool("Jesse")  # Non-empty strings evaluate to True in Python
True
>>> bool("")  # Empty strings evaluate to False
False
>>>
>>> ("Jesse" or "jesse")
'Jesse'
>>> ("" or "jesse")
'jesse'
>>>

This means that the expression:

这意味着表达式:

name == ("Jesse" or "jesse")

is basically equivalent to writing this:

基本上相当于写这个:

name == "Jesse"


In order to fix your problem, you can use the inoperator:

为了解决您的问题,您可以使用in运算符

# Test whether the value of name can be found in the tuple ("Jesse", "jesse")
if name in ("Jesse", "jesse"):

Or, you can lowercase the value of namewith str.lowerand then compare it to "jesse"directly:

或者,您可以将namewith的值小写str.lower,然后"jesse"直接与它进行比较:

# This will also handle inputs such as "JeSSe", "jESSE", "JESSE", etc.
if name.lower() == "jesse":

回答by Sukrit Kalra

if name in ("Jesse", "jesse"):

would be the correct way to do it.

将是正确的方法。

Although, if you want to use or, the statement would be

虽然,如果你想使用or,语句将是

if name == 'Jesse' or name == 'jesse':


>>> ("Jesse" or "jesse")
'Jesse'

evaluates to 'Jesse', so you're essentially not testing for 'jesse'when you do if name == ("Jesse" or "jesse"), since it only tests for equality to 'Jesse'and does not test for 'jesse', as you observed.

评估为'Jesse',因此您实际上并没有测试'jesse'何时执行if name == ("Jesse" or "jesse"),因为正如您所观察到的那样,它仅测试与 的相等性'Jesse'而不测试'jesse'

回答by falsetru

If you want case-insensitive comparison, use loweror upper:

如果您想要不区分大小写的比较,请使用lowerupper

if name.lower() == "jesse":

回答by cdhowie

The oroperator returns the first operand if it is true, otherwise the second operand. So in your case your test is equivalent to if name == "Jesse".

or运算符返回第一个操作数,如果它是真实的,否则第二个操作数。因此,在您的情况下,您的测试等效于if name == "Jesse".

The correct application of orwould be:

的正确应用or是:

if (name == "Jesse") or (name == "jesse"):