Pythonwhile不循环
我是 Python 新手,无法理解以下代码:
i = 0
j = 0
while not(i < 3 or j == 5):
i = i + 1
j = j + 1
print(i)
返回 0,即使 j == 5 的逆(非)返回 true
回答
not(i < 3 or j == 5)
等价于(根据德摩根的二元性):
not(i < 3) and not(j ==5)
进一步简化为:
(i >= 3) and (j != 5)
所以因为i = j = 0这个条件不满足。
回答
i < 3 or j == 5
i<3因此返回 True ( )
not(i < 3 or j == 5) 返回 False 这意味着您的 while 循环根本不执行任何操作,最后您的代码打印 i 的值为 0