python - while循环没有终止好奇
问题描述
import math
x = 4
a = 1
while a<11:
if a**3 < math.floor(x/1000):
a+=1
为什么我的代码没有终止?也就是说,为什么我的代码没有返回a的最大值?
但是,如果我在 while 循环中添加 if 语句,那么它可以工作,这是为什么呢?
解决方案
对于a = 1
→ a ** 3 = 1
。
math.floor(x/1000)
→ math.floor(4/1000) = 0
。
if a**3 < math.floor(x/1000):
永远不会变成True
并且值a
不会增加。这就是为什么while-loop
不终止。
推荐阅读
- php - 如何在没有 pjax 的情况下使用分页而不重新加载主页
- django - 如何在 django 中对索引 db 进行分组
- javascript - 在 Node 中,Buffer.length 是静态属性吗?
- python - 与 xarray.Dataset.mean 冲突的值?
- ms-access - 从 Access 复制字段以在另一个访问对象中使用
- javascript - Ember websocket/新对象冲突解决方法破坏了hasMany promise
- javascript - 使用 JavaScript 从另一个函数引用对象的函数
- c++ - C++ 中的“内联”关键字
- java - 验证 ANDROID_HOME 是否已添加到您的路径中
- javascript - 在 windows.print 之后,我可以检测用户是否单击打印或取消?