Python:使用 float 时程序不断陷入无限循环

发布于 2024-12-01 15:00:46 字数 1299 浏览 0 评论 0原文

这是我原来的工作程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-12-08 15:00:46

每次通过 while-loop 时,rush_high_gain 都会变小。但 back_sliding 保持不变。如果通过循环的次数足够多(大约 15 次),rush_height_gain 可能会小于backsliding。那时 current_height 将变得越来越小,并且您将永远不会退出循环。

rush_high_gain gets smaller with each pass through the while-loop. But back_sliding stays constant. If enough passes are made through the loop (about 15), rush_height_gain may be less than backsliding. At that point current_height will get smaller and smaller, and you will never exit the loop.

停滞 2024-12-08 15:00:46

如果您临时在循环中添加一个 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 shrinking rush_height_gain, it eventually becomes smaller than back_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 reached required_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 if rush_height_gain becomes less than back_sliding. Alternatively, you could change the way you compute rush_height_gain so that it never becomes less than back_sliding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文