无限循环和扣除货币.2

发布于 2025-02-11 21:58:27 字数 1732 浏览 1 评论 0原文

我对这个循环有问题,但我不明白错误在哪里以及为什么会进入无限循环。

我需要通过收银员确定要退还给客户的变更量,并使用1、2、5、10、20和50美分的最少数量的硬币显示更改。

从理论上讲,我似乎已经很好地理解了,实际上,前两个操作是正确的,但是我认为,当它必须选择0.05的硬币时,它会进入无限的循环。 如果有人可以向我解释我出错的地方,我将感激不尽。

在我的时循环中,这个想法是获取tot_rest的副本,并将其与硬币的for循环进行比较。如果tot_rest_temp大于硬币,则将硬币总结在REST_TEMP中,并从tot_rest_temp中减去它。

我做REST_COIN.APPEND(M)查看它在REST中给出了多少个硬币。

coin = [0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
payment = 1.00
product_cost = 0.23

tot_rest = round(payment - product_cost,2) 
print("Resto: ", round(tot_rest,2))

rest_coin = []
resto_temp = float(round(0,2))                  #  sum of coins  
tot_rest_temp = round(tot_rest,2)               #  copy the tot_rest 

while payment != (tot_rest + resto_temp):
    for m in coin:
        # print("1 tot resto temp: ",tot_resto_temp)
        if tot_rest_temp > m:
            resto_temp += m
            rest_coin.append(m)
            tot_rest_temp -= m
            print("m: ",m)
            print("resto temp: ",resto_temp)
            print("tot resto temp: ",tot_rest_temp)
            continue
        else:
            break
            
print("Final resto temp",resto_temp)
print("Final coins",rest_coin)

我尝试使用十进制,但问题仍然存在。 因此,我决定将所有东西都放在厘米中,以为它可以解决。相反,问题给出了相同的结果。

coin = [50, 20, 10, 5, 2, 1]
payment = 100
product_cost = 23

我使用abs(付款!= abs(tot_rest + rest_temp)):

但这也无法解决。 另外:

payment > (tot_rest + rest_temp): 

给出相同的问题,唯一改变它的是印刷我:

Final rest_temp 70
Final coins [50, 20]

在这一点上,我没有更多的想法可以解决,我仍然是编程的初学者。 如果有人有任何想法 - 谢谢

I have a problem with this loop and I don't understand where the error is and why it goes to infinite loop.

I need to determine the amount of change to be returned to the customer by the cashier and display the change using the smallest possible number of coins of 1, 2, 5, 10, 20 and 50 cents.

In theory I seem to have reasoned well, in fact the first 2 operations are correct but, I think, it goes into infinite loop when it has to choose a coin of 0.05.
If anyone can explain to me where I am going wrong I will be grateful.

In my while loop, the idea is to take the copy of the tot_rest and compare it to the for loop of the coins. If tot_rest_temp is greater than the coin, then it sums the coin in rest_temp and subtracts it from tot_rest_temp.

I do rest_coin.append(m) to see how many and what kind of coins it gave in rest.

coin = [0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
payment = 1.00
product_cost = 0.23

tot_rest = round(payment - product_cost,2) 
print("Resto: ", round(tot_rest,2))

rest_coin = []
resto_temp = float(round(0,2))                  #  sum of coins  
tot_rest_temp = round(tot_rest,2)               #  copy the tot_rest 

while payment != (tot_rest + resto_temp):
    for m in coin:
        # print("1 tot resto temp: ",tot_resto_temp)
        if tot_rest_temp > m:
            resto_temp += m
            rest_coin.append(m)
            tot_rest_temp -= m
            print("m: ",m)
            print("resto temp: ",resto_temp)
            print("tot resto temp: ",tot_rest_temp)
            continue
        else:
            break
            
print("Final resto temp",resto_temp)
print("Final coins",rest_coin)

I tried using Decimal but the problem remains.
So I decided to put everything in centimals, thinking it would solve it. Instead the problem gives the same result.

coin = [50, 20, 10, 5, 2, 1]
payment = 100
product_cost = 23

I used abs(payment != abs(tot_rest + rest_temp)):

but that doesn't solve either.
Also:

payment > (tot_rest + rest_temp): 

gives the same problem, the only thing that changes is that it prints me:

Final rest_temp 70
Final coins [50, 20]

At this point I have no more ideas to solve, I am still a beginner in programming.
If anyone has any ideas--Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

感性 2025-02-18 21:58:27

在Python中,浮点数学可能会破坏。而不是使用!=,请尝试>。付款&gt时,您的way循环为; (TOT_REST + RESTO_TEMP):

Floating point math can be really broken in Python. Instead of using !=, try >. Your while loop would be while payment > (tot_rest + resto_temp):

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