Exercise 8:
Optimal Payment in Python
Write a program in Python which puts forward the optimal way of paying any sum of US dollars in cash using the fewest number of bills and coins. There are sufficient bills (hundred-dollar-bills, fifty-dollar-bills, twenty-dollar-bills, ten-dollar-bills, five-dollar-bills, two-dollar-bills, one-dollar-bills) and coins (quarters, dimes, nickles, and pennies). If a negative number is typed as the amount, the program will be terminated.
print("The Optimal Way of Paying a Sum of US Dollars") print("pythonize.ir \t Programmer: Mohammad Rajabpur") print("To exit the program, enter a negative amount.") print("--------------------------------------- \n") while True: n = float(input("n = ")) n = round(n, 2) if n < 0: print("The program is terminated. Goodbye!") break elif n == 0: print("$0 = ") print("No bills and coins!") print("--------------------------------------- \n") else: print("$", n, "= ") r = "" b100 = n // 100 if b100 == 1: r += str(int(b100)) + " hundred-dollar bill \n" elif b100 > 1: r += str(int(b100)) + " hundred-dollar bills \n" n = n % 100 b50 = n // 50 if b50 == 1: r += str(int(b50)) + " fifty-dollar bill \n" elif b50 > 1: r += str(int(b50)) + " fifty-dollar bills \n" n = n % 50 b20 = n // 20 if b20 == 1: r += str(int(b20)) + " twenty-dollar bill \n" elif b20 > 1: r += str(int(b20)) + " twenty-dollar bills \n" n = n % 20 b10 = n // 10 if b10 == 1: r += str(int(b10)) + " ten-dollar bill \n" elif b10 > 1: r += str(int(b10)) + " ten-dollar bills \n" n = n % 10 b5 = n // 5 if b5 == 1: r += str(int(b5)) + " five-dollar bill \n" elif b5 > 1: r += str(int(b5)) + " five-dollar bills \n" n = n % 5 b2 = n // 2 if b2 == 1: r += str(int(b2)) + " two-dollar bill \n" elif b2 > 1: r += str(int(b2)) + " two-dollar bills \n" n = n % 2 b1 = n // 1 if b1 == 1: r += str(int(b1)) + " one-dollar bill \n" elif b1 > 1: r += str(int(b1)) + " one-dollar bills \n" n = n % 1 * 100 q = n // 25 if q == 1: r += str(int(q)) + " quarter \n" elif q > 1: r += str(int(q)) + " quarters \n" n = n % 25 d = n // 10 if d == 1: r += str(int(d)) + " dime \n" elif d > 1: r += str(int(d)) + " dimes \n" n = n % 10 nk = n // 5 if nk == 1: r += str(int(nk)) + " nickel \n" elif nk > 1: r += str(int(nk)) + " nickels \n" p = n % 5 if p == 1: r += str(int(p)) + " penny \n" elif p > 1: r += str(int(p)) + " pennies \n" print(r) print("--------------------------------------- \n")
================ RESTART: C:\Optimal Payment.py =============== The Optimal Way of Paying a Sum of US Dollars pythonize.ir Programmer: Mohammad Rajabpur To exit the program, enter a negative amount -------------------------------------------- n = 1399 $ 1399.0 = 13 hundred-dollar bills 1 fifty-dollar bill 2 twenty-dollar bills 1 five-dollar bill 2 two-dollar bills -------------------------------------------- n = 0.81 $ 0.81 = 3 quarters 1 nickel 1 penny -------------------------------------------- n = 0 $0 = No bills and coins! -------------------------------------------- n = 95378462103 $ 95378462103.0 = 953784621 hundred-dollar bills 1 two-dollar bill 1 one-dollar bill -------------------------------------------- n = -1 The program is terminated. Goodbye!