首页 > 解决方案 > Python while 不循环

问题描述

我是 Python 新手,无法理解以下代码:

i = 0
j = 0
while not(i < 3 or j == 5):
  i = i + 1
  j = j + 1
print(i)

返回 0,即使 j == 5 的逆(非)返回 true

标签: python

解决方案


not(i < 3 or j == 5)

等价于(通过De Morgan 的对偶性):

not(i < 3) and not(j ==5)

进一步简化为:

(i >= 3) and (j != 5)

所以既然i = j = 0这个条件不满足。


推荐阅读