我最近刚刚加入 python3 HypeTrain。不过我只是想知道如何在布尔值上使用 if 语句。示例:
RandomBool = True
# and now how can I check this in an if statement? Like the following:
if RandomBool == True:
# DoYourThing
另外,我可以像这样切换布尔值吗?
RandomBool1 == True # Boolean states True
if # AnyThing:
RandomBool1 = False # Boolean states False from now on?
您可以随心所欲地更改布尔值。至于如果:
if randombool is True:
有效,但您也可以使用:
if randombool:
如果你想测试某些东西是否是假的,你可以使用:
if randombool is False
但你也可以使用:
if not randombool:
我想你也可以使用
if randombool is True:
elif randombool is False:
我认为你不需要使用等号,除非它是 int 或 float。
如果我错了请纠正我
根据 PEP 8,看起来我们应该只使用
if RandomBool:
或
if not RandomBool:
我相信
is
比较内存地址,PEP 8 说我们不应该将 bool 与 ==
进行比较:
请参阅此处PEP 8