我正在创建一些简单的代码,但是or运算符在while循环中不起作用,但在if语句中起作用,在while循环中,它将重复直到直到满足第一个条件为止,而忽略第二个条件。
例如:
x = 100
y = 100
z = 30
w = 20
while y >= 1 or x >=1:
something()
x -= z
y -= w
print(f"finished")
print(x,y)
并且x始终会远低于零,并且循环只会在满足第一个条件或y时结束
我尝试了一些语句,并且由于某种原因而起作用了,所以我真的很困惑。
Short-circuit evaluation of this condition evaluates
y >= 1
first. Which isTrue
for one more iteration thanx >= 1
is. So you get an execution like this:In the last iteration, though
x
has failed to meet the end condition,y
is still valid. And sincey >= 1
is evaluated first - and it evaluates toTrue
- the while loop runs for one more time.改变你的一会儿状况
你应该没事的