首页 > 解决方案 > while循环没有终止好奇

问题描述

import math

x = 4

a = 1
while a<11:
    if a**3 < math.floor(x/1000):
        a+=1

为什么我的代码没有终止?也就是说,为什么我的代码没有返回a的最大值?

但是,如果我在 while 循环中添加 if 语句,那么它可以工作,这是为什么呢?

标签: pythonwhile-loop

解决方案


对于a = 1a ** 3 = 1
math.floor(x/1000)math.floor(4/1000) = 0

if a**3 < math.floor(x/1000):永远不会变成True并且值a不会增加。这就是为什么while-loop不终止。


推荐阅读