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
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
- The first PIN in
pin_triesis wrong, so the program says so, and the second, correct PIN shows the menu. - The
elseon theforloop runs only if the loop ends withoutbreak, that is, if no PIN matched; it blocks the card and empties the session. - Each item in
sessionis a menu choice with an amount, standing in for what the customer would type. - A withdrawal larger than the balance is refused, so the Rs. 9000 request fails after the Rs. 2500 one has gone through.
- 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
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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises