计算变化的函数

Function that calculates change

我想编写一个函数来计算机器必须给客户的零钱。该函数接收产品的成本、客户给的钱和一个字典,里面有机器的钱。

该函数应给出最小的硬币和纸币集合,并考虑到可用资金。

还应避免任何一种钱用完;例如,如果它有 2 个 10 欧元和 8 个 5 欧元,则不应在同一找零中使用 2 个 10 欧元。

这是我的词典:

d = {0.01: 10,
 0.02: 5,
 0.05: 2,
 0.1: 10,
 0.2: 5,
 0.5: 2,
 1: 5,
 2: 5,
 5: 2,
 10: 4,
 20: 5,
 50: 1,
 100: 0,
 200: 0,
 500: 0,
 }

这是我目前的代码:

def change(cost, given, d):
last_change = 0
change = given - cost #calculates how much we own the customer
if change == 0: #if we don't own the customer anything
    return change
else:
    if change in d and d[change] != 0: #if change is in the dictionary and its value is not 0 we can give it to the customer
        return change
    else:
        euro = int(change)
        cent = change - euro #calculates if we have to give any cents
        if cent == 0: #if we only have to give bills
            for item in d:
                if item > last_change and item < change and d[item] != 0: #biggest bill we can give the customer
                    last_change = item

我不知道下一步该做什么。

这是一些伪代码 - 您需要填写一些详细信息:

   amount = ... the input amount ...
   change = {}
   for denomination in [500, 200, 100, 50, ...]:
     if amount == 0:
       break
     n = ... number of coins of this denomination to use
     change[denomination] = n         # store it
     amount = amount - n*denomination # subtract from amount
   # the dictionary change contains how to make the change
from math import *
dict1 = {0.01: 10,
 0.02: 5,
 0.05: 2,
 0.1: 10,
 0.2: 5,
 0.5: 2,
 1: 5,
 2: 5,
 5: 2,
 10: 4,
 20: 5,
 50: 1,
 100: 0,
 200: 0,
 500: 0,
 }

def change(cost, given, dict1):

    last_change = 0
    change = given - cost
    if change == 0: 
        print change
    else:
        if change in dict1 and dict1[change] != 0: 
        print change
        else:

        if change >= 500 and dict1[change] != 0:
            a = floor( change / 500 )
            print a, " --500 's"
            change = change - ( a * 500 )

        if change >= 200 and dict1[change] != 0:
            b = floor( change / 200 )
            print b, " --200 's"
            change = change - ( b * 200 )

        if change >= 100 and dict1[change] != 0:
            c = floor( change / 100 )
            print c, " --100 's"
            change = change - ( c * 100 )

        if change >= 50 and dict1[change] != 0:
            d = floor( change / 50 )
            print d, " --50 's"
            change = change - ( d * 50 )

        if change >= 20 and dict1[change] != 0:
            e = floor( change / 20 )
            print e, " --20 's"
            change = change - ( e * 20 )


        if change >= 10 and dict1[change] != 0:
            f = floor( change / 10 )
            print f, " --10 's"
            change = change - ( f * 20 )


        if change >= 5 and dict1[change] != 0:
            g = floor( change / 5 )
            print g, " --5 's"
            change = change - ( g * 5 )

        if change >= 2 and dict1[change] != 0:
            h = floor( change / 2 )
            print h, " --2 's"
            change = change - ( h * 2 )

        if change >= 1 and dict1[change] != 0:
            i = floor( change / 1 )
            print i, " --1 's"
            change = change - ( i * 1 )

        if change >= 0.5 and dict1[change] != 0:
            j = floor( change / 0.5 )
            print j, " --0.5 's"
            change = change - ( j * 0.5 )

        if change >= 0.2 and dict1[change] != 0:
            k = floor( change / 0.2 )
            print k, " --0.2 's"
            change = change - ( k * 0.2 )


                   ---------------------------
                   ---------------------------
                   ---------------------------
                   ---------------------------
           implement similar steps for 0.1,0.05,0.02,0.01

输出将是这样的:

1  - 500 's
2  - 200 's
2  - 100 's
1  - 20 's