Exercises

Exercise 9:

Cash Dispenser in Python

There is an ATM which can dispense 100-dollar bills, 50-dollar-bills, and 20-dollar-bills. Write a program in Python which first receives the number of available bills. Then the customers withdraw their desired amounts based on available bills and each time an amount is withdrawn, the number of available bills is balanced. If the amount doesn't match the available bills, the error message is shown to the customer. If the ATM runs out of bills, an apology message is displayed and the program is terminated.


Code:
 
print("ATM Dispenser")
print("pythonize.ir \t Programmer: Mohammad Rajabpur")
print("--------------------------------------------------------------------------------\n")
b100 = int(input("The number of hundred-dollar-bills = "))
b50 = int(input("The number of fifty-dollar-bills = "))
b20 = int(input("The number of twenty-dollar-bills = "))

while True:
    print("--------------------------------------------------------------------------------\n")
    print("Existing Bills:")
    print(b100, "hundered-dollar-bill(s) +", b50, "fifty-dollar-bill(s) +", b20, "twenty-dollar-bill(s)")
    s = int(input("Amount = "))
    if b100 == 0 and b50 == 0 and b20 == 0:
        print("Sorry: The ATM is out of cash.")
        break
    flag = 0
    for i in range((b100*100)//100, -1, -1):
        if flag == 1:
            break
        for j in range((b50*50)//50, -1, -1):
            if flag == 1:
                break
            for k in range((b20*20)//20, -1, -1):
                if i*100 + j*50 + k*20 == s:
                    print(i, "hundered-dollar-bill(s) +", j, "fifty-dollar-bill(s) +", k, "twenty-dollar-bill(s)")
                    flag = 1
                    b100 -= i
                    b50 -= j
                    b20 -= k
                    break
    if flag == 0:
        print("The amount doesn't match the existing bills.")

Run:
================= RESTART: C:\ATM Dispenser.py ================
ATM Dispenser
pythonize.ir 	 Programmer: Mohammad Rajabpur
--------------------------------------------------------------------------------

The number of hundred-dollar-bills = 10
The number of fifty-dollar-bills = 10
The number of twenty-dollar-bills = 10
--------------------------------------------------------------------------------

Existing Bills:
10 hundered-dollar-bill(s) + 10 fifty-dollar-bill(s) + 10 twenty-dollar-bill(s)
Amount = 280
2 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 4 twenty-dollar-bill(s)
--------------------------------------------------------------------------------

Existing Bills:
8 hundered-dollar-bill(s) + 10 fifty-dollar-bill(s) + 6 twenty-dollar-bill(s)
Amount = 800
8 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 0 twenty-dollar-bill(s)
--------------------------------------------------------------------------------

Existing Bills:
0 hundered-dollar-bill(s) + 10 fifty-dollar-bill(s) + 6 twenty-dollar-bill(s)
Amount = 280
0 hundered-dollar-bill(s) + 4 fifty-dollar-bill(s) + 4 twenty-dollar-bill(s)
--------------------------------------------------------------------------------

Existing Bills:
0 hundered-dollar-bill(s) + 6 fifty-dollar-bill(s) + 2 twenty-dollar-bill(s)
Amount = 300
0 hundered-dollar-bill(s) + 6 fifty-dollar-bill(s) + 0 twenty-dollar-bill(s)
--------------------------------------------------------------------------------

Existing Bills:
0 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 2 twenty-dollar-bill(s)
Amount = 30
The amount doesn't match the existing bills.
--------------------------------------------------------------------------------

Existing Bills:
0 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 2 twenty-dollar-bill(s)
Amount = 40
0 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 2 twenty-dollar-bill(s)
--------------------------------------------------------------------------------

Existing Bills:
0 hundered-dollar-bill(s) + 0 fifty-dollar-bill(s) + 0 twenty-dollar-bill(s)
Amount = 540
Sorry: The ATM is out of cash.