本文介绍了为什么这个 python while 循环没有结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么这段代码似乎无限循环?逻辑,while not False = while True
,而这个 True 指的是 100 <0
这是错误的,因此它应该执行打印语句,对吗?为什么它会卡在循环中呢..?
I am wondering why this code seems to loop infinitely? The logic, while not False = while True
, and this True is referring to 100 < 0
which is false, hence it should execute the print statement instead, right? Why is it stuck in the loop then..?
num = 100
while not False:
if num < 0:
break
print('num is: ' + str(num))
推荐答案
简短回答:由于以下内容不正确,您的 if
子句的内容将不会被评估,因此 不执行 break
.
Short answer: Since the following is not true, the contents of your if
clause will not be evaluated, hence the break
is not performed.
if num < 0:
真正运行的是以下内容:
All that is really running is the following:
num = 100
while not False:
if num < 0: #False
#what is here is unimportant, since it will never run anyway.
...或者一路"简化它,这就是你正在做的:
...or to simplify it "all the way", this is what you're doing:
while true:
if false:
break
这篇关于为什么这个 python while 循环没有结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!