繁体   English   中英

找出python中的货币变化

[英]Figuring out money change in python

我有一个任务,我必须提示用户输入产品的成本和支付的金额,我必须输出一分钱、一角钱、四分之一、1 美元、5 美元、20 美元、50 美元和 100 美元的变化,例如:项目是 19.99 美元,客户支付 50 美元的账单。 提供的找零是一张 20 美元的钞票、一张 10 美元的钞票和一分钱。 我很困惑如何获得这样的输出,任何帮助将不胜感激,这是我目前所拥有的

cost = float(input('Cost: '))
amount_paid = float(input('Amount paid: '))
penny = 0.01
dime = 0.10
quarter = 0.25
dollar_1 = 1.00
dollar_5 = 5.00
dollar_10 = 10.00
dollar_20 = 20.00
dollar_50 = 50.00
dollar_100 = 100.00
change = cost - amount_paid
if amount_paid < cost:
    print('Error')

我不知道接下来要做什么

这里的一个常见错误是使用浮点数。 您应该将所有内容转换为最小的整数单位(一分)并使用整数数学。 浮点数学是……模糊的。

currencies = {"penny": 1,
              "nickel": 5,
              "dime": 10,
              "quarter": 25,
              "dollar": 1_00,
              "five": 5_00,
              "ten": 10_00,
              "twenty": 20_00,
              "fifty": 50_00,
              "hundred": 100_00}
# never seen that numeric notation before? It's safe to embed underscores
# in numerical literals! It's often used for large numbers in place of
# commas, but it makes sense here in place of a period.

那么你应该只需要为结果定义一个字典,并使用divmod来查找剩余到期金额中可以容纳多少面额。

change_due = {}

for denomination, amt in reversed(currencies.items()):
    if amt < amt_due:
        d, m = divmod(amt_due, amt)
        change_due[denomination] = d
        amt_due = m

欢迎来到 stackoverflow! 我为你写了代码,这就是它的工作原理。 基本上它会看到每种货币并使用整数除法//来查看可以容纳多少整数。然后从剩余的变化中减去该金额,然后过程继续。 如果您有什么不明白的地方,或者您认为有错误,请询问。 代码:

cost = float(input('Cost: '))
amount_paid = float(input('Amount paid: '))
penny = 0.01
dime = 0.10
quarter = 0.25
dollar_1 = 1.00
dollar_5 = 5.00
dollar_10 = 10.00
dollar_20 = 20.00
dollar_50 = 50.00
dollar_100 = 100.00
changeTypes = {dollar_100:0,dollar_50:0,dollar_20:0,dollar_10:0,dollar_5:0,dollar_1:0,quarter:0,dime:0,penny:0}
change = amount_paid-cost
if amount_paid < cost:
    print('Error: InsufficientFunds')
for changeType in changeTypes:
    numAmount = max(0,change//changeType)
    change-=numAmount*changeType
    changeTypes[changeType] = int(numAmount)


print(changeTypes)

PS你应该把它变成一个函数,它不应该太难。

你可以用字典来做这件事,但不使用仍然有很多方法可以解决这个问题,选择无穷无尽,这是一个想法

def get_bills(change, value):
    if change//value > 0:
        bills = change//value
        change -= bills * value
        return bills, change
    else:
        return 0, change

cost = float(input('Cost: '))
paid = float(input('Amount Paid: '))
while paid < cost:
    paid = float(input('Amount Paid: '))

change = paid - cost

hundreds, change, = get_bills(change, 100)

fifties, change, = get_bills(change, 50)

twenties, change = get_bills(change, 20) 

tens, change = get_bills(change, 10) 

fives, change = get_bills(change, 5)

ones, change = get_bills(change, 1)

quarters, change = get_bills(change, .25)

dimes, change = get_bills(change, .1) 

nickels, change = get_bills(change, .05)

pennies = round(change * 100)

print(f"Hundreds: {hundreds}, Fifties: {fifties}, Twenties: {twenties}," +
      f" Tens: {tens}, Fives: {fives}, Ones: {ones}, Quarters: {quarters}," +  
      f" Dimes: {dimes}, Nickels: {nickels}, Pennies: " +
      f"{pennies}")
    bill = float(input())
paid = float(input())
Available = {100.0:0,50.0:0,20.0:0,10.0:0,5.0:0,1.0:0,0.25:0,0.10:0,0.01:0}
due = paid-bill
for change in sorted(Available,reverse = True):
    amt= max(0,due//change)
    due-=amt*change
    Available[change] = int(amt)
print(Available)

我知道这是一个迟到的回应,但也许它可以帮助某人。

下面是一个代码,可以做你想做的事情。 该程序从最大到最小迭代可用的注释,并计算当前注释可用于从要给出的剩余更改中扣除多少次。

最后返回一个包含用于达到所需总和的注释的列表。

# Available notes for change
notes = [500, 200, 100, 50, 20, 10]


def change_notes(change, notes):
    notes_out = []
    for note in notes:
        print(f"current note is {note}")
        sleep(1)
        while change > 0 and note <= change:
            if change - note >= 0:
                change -= note
                notes_out.append(note)
                print(f"change is {change}")
                sleep(1)
        if change == 0:
            break
            

    return notes_out

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM