【问题标题】:Figuring out money change in python在 python 中找出货币变化
【发布时间】:2018-09-19 04:02:21
【问题描述】:

我有一个任务,我必须提示用户输入产品的成本和支付的金额,我必须输出硬币、硬币、25 美分、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')

我不知道下一步该做什么

【问题讨论】:

  • 等等...什么,elif之后的内容是什么???
  • 顺便说一句,你的问题不清楚
  • 你的问题到底是什么?
  • 浮点数不适合使用货币。最好在便士中使用整数值。
  • 我如何获得以美元为单位的输出和/或像示例一样的变化,而不仅仅是像 10.00 这样的数字

标签: python currency


【解决方案1】:

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

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

【讨论】:

  • 嗨,亚当·斯密,对货币词典投了赞成票。但是 dict 项是不可逆的
【解决方案2】:

欢迎使用 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)

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

【讨论】:

  • 谢谢,喜欢并支持您的回答。你是 9 年级学生的事实给我留下了深刻的印象,继续努力:)
【解决方案3】:

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

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}")

【讨论】:

    【解决方案4】:
        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)
    

    【讨论】:

    • 对可用字典进行了排序,因为 python 在运行时对字典进行了洗牌,这可能会通过使用最小值而不是最大值跳过更改来产生意想不到的结果
    【解决方案5】:

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

    以下是完全按照您的意愿行事的代码。程序从最大到最小迭代可用的注释,并计算当前注释可用于从剩余的更改中扣除多少次。

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

    # 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多