简体   繁体   English

为什么“和”条件在我的 Python 代码中不起作用?

[英]Why doesn't the "and" condition work in my Python codes?

Each separate statement is true, but why is the combination of the two with "and" false?每个单独的陈述都是正确的,但是为什么将两者与“和”结合起来是错误的? codes代码

In python and is a logical AND that returns True if both the operands are true whereas '&' is a bitwise operator.在 python 中,and 是一个逻辑 AND,如果两个操作数都为真,则返回 True,而 '&' 是按位运算符。 I think you are trying to to something like this.我认为你正在尝试这样的事情。

if (other_list[0] == 0 and rand_vec[0] == 1):
    # Do something

Many thanks to Pw Wolf and all others.非常感谢 Pw Wolf 和所有其他人。 Now I understand it.现在我明白了。

Original code:原始代码:

if (other_list[0] == 0 & rand_vec[0] == 1):

    # do something 

Improved code:改进的代码:

if (other_list[0] == 0 and rand_vec[0] == 1):

    # do something 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM