Python:使用 float 时程序不断陷入无限循环
这是我原来的工作程序:
def num_rushes(required_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
while current_height < required_height:
rushes += 1
current_height += rush_height_gain
if current_height >= required_height:
break
else:
current_height -= back_sliding
return rushes
print num_rushes(100, 15, 7)
这个程序的作用是计算每次冲刺(rush_height_gain
)后我们达到所需高度(required_height
)需要多少次“冲刺” ,我们后退给定量(back_sliding
)。
我想要的是在每次冲刺之后,将获得的高度乘以 0.95(我们每次都会损失 5% 的冲刺)。
正如你在下面看到的,我所做的是将rush_height_gain转换为浮点数,然后将行rush_height_gain *= 0.95
添加到我的while循环中,但是由于某种原因,我似乎无法弄清楚,我最终陷入无限循环。
def num_rushes(required_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
rush_height_gain = float(rush_height_gain)
while current_height < required_height:
rushes += 1
current_height += rush_height_gain
if current_height >= required_height:
break
else:
current_height -= back_sliding
rush_height_gain *= 0.95
return rushes
print num_rushes(100, 15, 7)
任何关于为什么会发生这种情况以及如何获得我想要的结果的帮助将不胜感激。
干杯。
Here is my original working program:
def num_rushes(required_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
while current_height < required_height:
rushes += 1
current_height += rush_height_gain
if current_height >= required_height:
break
else:
current_height -= back_sliding
return rushes
print num_rushes(100, 15, 7)
What this program does it calculates how many "rushes" it takes until we reach the desired height (required_height
), after each rush (rush_height_gain
), we fall back by a given amount (back_sliding
).
What I am trying to is after each rush, is multiply the height gained by 0.95 (we lose 5% of the rush each time).
As you can see below, what I have done is converted rush_height_gain to a float and then added the line rush_height_gain *= 0.95
into my while loop, however for some reason, which I cannot seem to figure out, I end up with an infinite loop.
def num_rushes(required_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
rush_height_gain = float(rush_height_gain)
while current_height < required_height:
rushes += 1
current_height += rush_height_gain
if current_height >= required_height:
break
else:
current_height -= back_sliding
rush_height_gain *= 0.95
return rushes
print num_rushes(100, 15, 7)
Any help as to why this occurs and how can I get my desired result would be greatly appreciated.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次通过
while-loop
时,rush_high_gain
都会变小。但back_sliding
保持不变。如果通过循环的次数足够多(大约 15 次),rush_height_gain
可能会小于backsliding
。那时current_height
将变得越来越小,并且您将永远不会退出循环。rush_high_gain
gets smaller with each pass through thewhile-loop
. Butback_sliding
stays constant. If enough passes are made through the loop (about 15),rush_height_gain
may be less thanbacksliding
. At that pointcurrent_height
will get smaller and smaller, and you will never exit the loop.如果您临时在循环中添加一个
print
语句来显示每次迭代时相关变量的值,您会发现,由于您不断缩小rush_height_gain
,它最终会变得更小比back_sliding
。一旦发生这种情况,物体每一步向后滑动的距离都会比上升的距离远,因此您实际上会失去高度。这意味着,如果到那时您还没有达到required_height
,那么您永远也不会达到。最简单的解决方案可能是更改循环条件,以便在
current_height
变为负数或rush_height_gain
小于back_sliding
时停止。或者,您可以更改计算rush_height_gain
的方式,使其永远不会小于back_sliding
。If you temporarily add a
print
statement into your loop to show the values of relevant variables at each iteration, you'll see that because you keep shrinkingrush_height_gain
, it eventually becomes smaller thanback_sliding
. Once that happens, the object slides back further than it goes up at each step, so you wind up actually losing height. That means that if you haven't reachedrequired_height
by that point, you never will.Probably the easiest solution is to change your loop condition to stop if
current_height
becomes negative, or alternatively ifrush_height_gain
becomes less thanback_sliding
. Alternatively, you could change the way you computerush_height_gain
so that it never becomes less thanback_sliding
.