有一个这样的结构:
if a == 1 and b == 1 and c == 1 or c > 5:
do_something()
问题:该条件将如何处理or?我do_something()只需要在所有 4 个条件都为真时执行,但 python 不解释这个条件
if a == 1 and b == 1 and c == 1 or c > 5
像两个:
如果 2 个条件中的 1 个为真a==1 and b==1 and c==1,and c>5and 将执行?
ps - 你需要通过......do_something()c==1 or c>5or
操作优先级: https ://wombat.org.ua/AByteOfPython/operators_and_expressions.html#id5
首先,执行逻辑“AND”(
and),然后执行逻辑“OR”(or)。这样:false and false and false or true=>false and false or true=>false or true=>truefalse and true and true or false=>false and true or false=>false or false=>falsetrue and true and true or false=>true and true or false=>true or false=>true也就是说:为了让整个条件返回
true,您必须要么拥有前三个操作数true,要么拥有最后一个,要么同时拥有所有操作数。