Python的OR操作行为

huangapple go评论79阅读模式
英文:

Python OR behavior

问题

我遇到了一个问题,我试图为一个输入制作别名。
inp = "h" #输入
如果输入 == "s":
打印("站立")
elif 输入 == "h":
打印("击中")
否则:
打印("其他")

这段代码的行为正如预期的那样,s会使其进入第一个分支,h会进入第二个分支,其他任何输入都会进入第三个分支。但是当我将or加入到if语句中时:

if 输入 == "s" or "站立":
    打印("站立")
elif 输入 == "h":
    打印("击中")
否则:
    打印("其他") ```

不管输入是什么,都会选择第一个分支。我期望的是得到与以前相同的逻辑。我是不是误用了OR,或者这应该可以工作但却没有?

<details>
<summary>英文:</summary>

I ran into an issue while trying to make aliases for an input.

inp = "h" #the input
if inp == "s":
print("stand")
elif inp == "h":
print("hit")
else:
print("else")

This code behaves exactly as expected *s* makes it take the first branch *h* second and anything else the third. But when i add **or** into the if statement

inp = "h"
if inp == "s" or "stand":
print("stand")
elif inp == "h":
print("hit")
else:
print("else")

the first branch gets picked regardless on the input provided. What I expected to happen was to get the same logic as before. Am I misusing OR or this should work but doesnt?

</details>


# 答案1
**得分**: 1

你没有*完全*正确使用它。我在第一次使用逻辑时也犯了同样的错误,所以不用太担心。正确的用法并没有太大的区别:
```python
inp = "h"
if inp == "s" or inp == "stand":
    print("stand")
elif inp == "h":
    print("hit")
...

条件工作的方式是对于每个条件,Python 基本上在末尾添加了 is True。所以 if inp == "s" 基本上就是 if inp == "s" is True。这可以解释为什么输入 stand 不起作用,因为它会检查 "stand" 是否等于 True。

编辑:抱歉,我上面的说法是不正确的。程序正在检查的是 "stand" 是否是一个 "有效" 项目。这可能不是正确的术语,但不有效的东西可能是一个空列表、空字符串或关键字 None。因为 "stand" 是有效的,所以它被视为 True,因此总是执行第一个分支。

英文:

You're not quite using or correctly. I made the same mistake with my first time using logic, so I wouldn't stress it too much. The correct usage isn't much different:

inp = &quot;h&quot;
if inp == &quot;s&quot; or inp == &quot;stand&quot;:
    print(&quot;stand&quot;)
elif inp == &quot;h&quot;:
    print(&quot;hit&quot;)
...

The way conditions work is that for each condition, python basically puts on is True at the end. So if inp == &quot;s&quot; would basically be if inp == &quot;s&quot; is True. That would explain why inputting stand wouldn't work, since it would be checking if "stand" was equal to True.

Edit: sorry, my above statement is incorrect. What the program is checking for is if "stand" is a "valid" item. That might not be the correct terminology, but something that wouldn't be valid would be an empty list, empty string, or the keyword None. Since "stand" is valid, it counts as being True, and thus the first branch is always taken.

答案2

得分: 0

你误解了 or 语法

在 Python 中,所有空字符串、列表、字典等都会被求值为 False

x = []
if x: # 求值为 False
   pass

在这个例子中,x 是空的,因此为 False。根据这种行为,非空 列表、字符串等会被求值为 True

x = [1, 2, 3]
if x: # 求值为 True!
   print("这将会打印!")

你的代码在询问 inp 是否等于 &quot;s&quot;或者字符串 &quot;stand&quot; 非空。字符串 &quot;stand&quot; 是非空的,因此该语句求值为 True。要修正这个问题,尝试询问 inp 是否等于 &quot;s&quot;,或者是否等于 &quot;stand&quot;

inp = &quot;h&quot;
if inp == &quot;s&quot; or inp == &quot;stand&quot;: # 注意修改... 这将会求值为 False。
    print(&quot;stand&quot;)
elif inp == &quot;h&quot;:
    print(&quot;hit&quot;)
else:
    print(&quot;else&quot;)
英文:

You've misunderstood the or syntax

In python all empty strings, lists, dicts, etc, evaluate to False:

x = []
if x: # Evaluates to False
   pass

In this example, x, being empty is False. According to this behavior any non-empty list, string, etc, evaluates to True:

x = [1, 2, 3]
if x: # Evaluates to True!
   print(&quot;This will print!&quot;)

Your code is asking is inp equal to &quot;s&quot;, or is the string &quot;stand&quot; non-empty. The string &quot;stand&quot; is non-empty and thus the statement evaluates to True. To fix this, try asking is inp equal to &quot;s&quot;, or is inp equal to &quot;stand&quot;:

inp = &quot;h&quot;
if inp == &quot;s&quot; or inp == &quot;stand&quot;: # Note the edit... This will evaluate to False.
    print(&quot;stand&quot;)
elif inp == &quot;h&quot;:
    print(&quot;hit&quot;)
else:
    print(&quot;else&quot;)

huangapple
  • 本文由 发表于 2023年5月21日 07:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定