简体   繁体   English

找出python中的货币变化

[英]Figuring out money change in python

I have an assignment where I have to prompt the user for cost of a product and amount paid, I have to output change in pennies, dimes, quarters, $1, $5, $20, $50, and $100, for example: The cost of the item is $19.99 and the client pays with a $50 bill.我有一个任务,我必须提示用户输入产品的成本和支付的金额,我必须输出一分钱、一角钱、四分之一、1 美元、5 美元、20 美元、50 美元和 100 美元的变化,例如:项目是 19.99 美元,客户支付 50 美元的账单。 The change to be provided is 1 $20 bill, one $10 bill, and one penny.提供的找零是一张 20 美元的钞票、一张 10 美元的钞票和一分钱。 I am confused how to get an output like that though, any help would be greatly appreciated, heres what I have so far我很困惑如何获得这样的输出,任何帮助将不胜感激,这是我目前所拥有的

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')

I dont know what to do next我不知道接下来要做什么

A common misstep here is to use floats.这里的一个常见错误是使用浮点数。 You should instead convert everything to the smallest whole unit (a cent) and use integer math.您应该将所有内容转换为最小的整数单位(一分)并使用整数数学。 Floating point math is...fuzzy.浮点数学是……模糊的。

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.

Then you should only need to define a dictionary for the result, and use divmod to find how many of the denomination can fit in the amount left due.那么你应该只需要为结果定义一个字典,并使用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

welcome to stackoverflow!欢迎来到 stackoverflow! I wrote the code for you and this is how it works.我为你写了代码,这就是它的工作原理。 Basically it sees each currency and uses integer division // to see how many integers can fit in. It then subtracts that amount from the remaining change and the process continues.基本上它会看到每种货币并使用整数除法//来查看可以容纳多少整数。然后从剩余的变化中减去该金额,然后过程继续。 Please ask if you don't understand something, or if you think there is an error.如果您有什么不明白的地方,或者您认为有错误,请询问。 Code:代码:

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 you should make this a function, it shouldn't be too hard. PS你应该把它变成一个函数,它不应该太难。

You could do this good with dictionaries, but without using still there are many ways to go about this, options are endless, heres one idea你可以用字典来做这件事,但不使用仍然有很多方法可以解决这个问题,选择无穷无尽,这是一个想法

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)

I know this is a late response but maybe it can help someone.我知道这是一个迟到的回应,但也许它可以帮助某人。

Below is a code for doing exactly what you want.下面是一个代码,可以做你想做的事情。 The program iterates through the notes available from largest to smallest and calculates how many times the current note may be used to deduct from the remaining change to be given.该程序从最大到最小迭代可用的注释,并计算当前注释可用于从要给出的剩余更改中扣除多少次。

Finally returning a list containing the notes used to reach the sum required.最后返回一个包含用于达到所需总和的注释的列表。

# 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