def func(x):
if x %3 != 0 & x % 5 == 0:
return "five" ## if divisible by only 5, return five
print(func(181)) ## returns "five" instead of returning None
现在,翻转操作数的相同代码:
def func(x):
if x % 5 == 0 & x % 3 != 0
return "five"
print(func(181)) ## returns None as intended and correct
有人可以解释一下这个条件流程吗?我似乎不太明白。在第一个场景中,我们知道 x(181) 不能被 3 整除 - 所以它检查条件的第二部分。 181 % 5 不是 0,那么为什么仍然打印 5 而不是退出 if 语句? 在第二种情况下,我们一开始就知道 181%5 不是 0,所以我知道我们可以完全短路掉那段代码。我明白为什么第二种情况可以正常工作,但为什么第一种情况不能正常工作,不管它有多低效?