猿问

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

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


thing1 = True

thing2 = 3


if thing2 == 3 & thing1:

   print "hi"


呼如林
浏览 555回答 2
2回答

慕姐4208626

您想使用逻辑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"
随时随地看视频慕课网APP

相关分类

Python
我要回答