在Python中,我可以编码并让它按预期工作吗?Python“and”and“or”if语句
if a != None and b == None or a == None and b != None
基本上我想看看,如果A或B是无,而另一种是不
在Python中,我可以编码并让它按预期工作吗?Python“and”and“or”if语句
if a != None and b == None or a == None and b != None
基本上我想看看,如果A或B是无,而另一种是不
在Python,有可能是NoneType
只有一个实例可以存在,所以你可以使用is
运营商,这样
if (a is None and b is not None) or (b is None and a is not None):
你也可以算的None
就像这个
if (a, b).count(None) == 1:
0123次数
或者你可以明确地检查,像这样
if a != b and (a is None or b is None):
因为它听起来像你想要一个xor
...
if (a is None) != (b is None):
...
+1你可以这样写:if(a是None)不是(b是None):' – thefourtheye
侧面说明,比较无的“正确”的方式通常是使用' '''',即''a不是无,b是无...''。除此之外,你的代码应该按照预期工作。 – Moritz