Answer to Question #298379 in Python for Christine

Question #298379

GIVE ME MY CHANGE in PYTHON!




The coins and bills in the existing Philippine Monetary System are




given below.




Coins




Bills




5 centavos




10 centavos




20 pesos




50 pesos




25 centavos




1 peso




100 pesos




200 pesos




5 pesos




10 pesos




500 pesos




1000 pesos




Given a change in pesos, create a Python script which will decompose the input CHANGE in terms of the largest possible amount in bills/coins.

1
Expert's answer
2022-02-16T04:45:15-0500
coins = [50, 25, 10, 5]
bills = [1000, 500, 200, 100, 50, 20, 10, 5, 1]


def get_list(val, noms):
    L = []
    for i in range(len(noms)):
        count = 0
        while val >= noms[i]:
            val -= noms[i]
            count += 1
        L.append(count)
    return L



def get_change(amount):
    pesos = int(amount)
    centavos = round((amount - pesos) * 100)
    
    x = centavos % 5
    if x > 2:
        centavos += 5-x
 
    L1 = get_list(pesos, bills)
    L2 = get_list(centavos, coins)


    return L1, L2


def print_change(L1, L2):
    for i in range(len(L1)):
        if L1[i]:
            s = 'bill' if L1[i] == 1 else 'bills'
            if bills[i] == 1:
                print(L1[i], '1 peso', s)
            else:
                print(L1[i], bills[i], 'pessos', s)
    
    for i in range(len(L2)):
        if L2[i]:
            s = 'coin' if L2[i] == 1 else 'coins'
            print(L2[i], coins[i], 'centavos', s)


def main():
    amount = float(input('Enter a change in pesos: '))
    L1, L2 = get_change(amount)
    print('Your charge is:')
    print_change(L1, L2)


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS