0

我正在做一些功课,我对此感到头疼..要么我很愚蠢,要么我太累了,无法做到这一点..我设法使输出正确并确定了优先级,但是当我输入一个浮点数时,它就会消失疯狂..总体目标是能够输入一个可以工作的浮点值..我似乎无法理解数学解决方案

price = input("Enter Price ")
cash = input("Enter Cash ")
coins = [100, 50, 20, 10, 5, 1, 0.5]
change = cash-price
i = 0
while i<len(coins):
    print int(change/coins[i]),str(" X "),coins[0+i]
    if change>0:
        change = change-((change/coins[i])*coins[i])
    else:
        change = max(change,0)
    i=i+1

谢谢 !

4

1 回答 1

0

我懒得去检查数学,但似乎你在从零钱中减去时忘记了浮点数,所以部分硬币被视为当前的全部金额,一切都归零。我添加了一个int

price = input("Enter Price ")
cash = input("Enter Cash ")
coins = [100, 50, 20, 10, 5, 1, 0.5]
change = cash-price
i = 0
while i<len(coins):
    print int(change/coins[i]),str(" X "),coins[0+i]
    if change>0:
        change = change-(int(change/coins[i])*coins[i])
    else:
        change = max(change,0)
    i=i+1
于 2012-11-01T20:39:41.867 回答