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

Roh*_*ria 4 python if-statement boolean

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

thing1 = True
thing2 = 3

if thing2 == 3 & thing1:
   print "hi"
Run Code Online (Sandbox Code Playgroud)

pra*_*nsg 6

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

if thing2 == 3 and thing1:
   print "hi"
Run Code Online (Sandbox Code Playgroud)

因为你已经使用过&,错误突然出现了,说:

TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number'
                                           ^^^^^^
Run Code Online (Sandbox Code Playgroud)