繁体   English   中英

在Python中的If语句中使用布尔值

[英]Using a Boolean in an If-Statement in Python

我有一些带有if语句的代码,其中一个条件是布尔值。 但是,CodeSkulptor说“第36行:TypeError:BitAnd不支持的操作数类型:'bool'和'number'”。 如果可以的话请帮忙。 这就是那段代码的样子。 (我只是更改了所有变量名称以及if语句执行的内容)

thing1 = True
thing2 = 3

if thing2 == 3 & thing1:
   print "hi"

你想使用逻辑and (而不是& ,这是Python中的按位AND运算符):

if thing2 == 3 and thing1:
   print "hi"

因为你使用了& ,所以出现了错误,说:

TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number'
                                           ^^^^^^

&是按位AND运算符。 你想使用逻辑and来代替:

if thing2 == 3 and thing1:
    print "hi"

暂无
暂无

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

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