Editor

Python Program for an ATM Machine

Intermediate · Basics

This program works like a simple ATM: it checks the PIN, then lets the customer check the balance, withdraw cash and deposit money from a menu. Here the customer's key presses come from a list, so the program runs a short session on its own, including a wrong PIN and a withdrawal that is too large.

Program

atm_machine.py
balance = 10000
PIN = 1234
# What the customer types, in order. A real ATM would ask each time:
# pin = int(input("Enter PIN: ")) and choice = input("Choose 1-4: ")
pin_tries = [4321, 1234]
session = [("1", 0), ("2", 2500), ("2", 9000), ("3", 1500), ("1", 0), ("4", 0)]
for pin in pin_tries:
if pin == PIN:
print("PIN accepted. Welcome!")
print("Menu: 1 Balance | 2 Withdraw | 3 Deposit | 4 Exit")
break
print("Wrong PIN, please try again")
else:
print("No correct PIN entered, card blocked")
session = [] # nothing else happens
for choice, amount in session:
if choice == "1":
print("Choice 1: your balance is Rs.", balance)
elif choice == "2" and amount > balance:
print(f"Choice 2: cannot withdraw Rs. {amount}, the balance is only Rs. {balance}")
elif choice == "2":
balance -= amount
print(f"Choice 2: withdrew Rs. {amount}, please take your cash")
elif choice == "3":
balance += amount
print(f"Choice 3: deposited Rs. {amount}")
elif choice == "4":
print("Choice 4: thank you for using the ATM. Goodbye!")
break
Output
Wrong PIN, please try again
PIN accepted. Welcome!
Menu: 1 Balance | 2 Withdraw | 3 Deposit | 4 Exit
Choice 1: your balance is Rs. 10000
Choice 2: withdrew Rs. 2500, please take your cash
Choice 2: cannot withdraw Rs. 9000, the balance is only Rs. 7500
Choice 3: deposited Rs. 1500
Choice 1: your balance is Rs. 9000
Choice 4: thank you for using the ATM. Goodbye!

How it works

  1. The first PIN in pin_tries is wrong, so the program says so, and the second, correct PIN shows the menu.
  2. The else on the for loop runs only if the loop ends without break, that is, if no PIN matched; it blocks the card and empties the session.
  3. Each item in session is a menu choice with an amount, standing in for what the customer would type.
  4. A withdrawal larger than the balance is refused, so the Rs. 9000 request fails after the Rs. 2500 one has gone through.
  5. Choice 4 ends the session with break, just as pressing Exit would at a real machine.

Another way to write it

Working out the notes to dispense

Working out the notes to dispense
amount = 3800 # or: amount = int(input("Amount to withdraw: "))
notes = [500, 200, 100]
if amount % 100 != 0:
print("Please enter a multiple of 100")
else:
print("Notes for Rs.", amount)
for note in notes:
count = amount // note
if count > 0:
print(f" Rs. {note} x {count}")
amount = amount % note
Output
Notes for Rs. 3800
  Rs. 500 x 7
  Rs. 200 x 1
  Rs. 100 x 1

Starting with the biggest note, amount // note is how many of that note fit and amount % note is what is left for the smaller notes, so Rs. 3800 comes out as seven 500s, one 200 and one 100.

Try it yourself

Make the ATM refuse any withdrawal that is not a multiple of 100.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises