Pythondex
Python Programs

ATM Program In Python With Source Code

Last updated April 25, 2023 by Jarvis Silva

If you’re looking to create an ATM program in Python, you’ve come to the right place, In this tutorial I will show you step by step process of developing an ATM program.

Our goal today is to develop a Python program that emulates an ATM, allowing users to withdraw cash and check their account balance, similar to the functionality provided by an actual ATM machine so let’s code it

Complete ATM Program Code In Python


# PYTHON ATM PROGRAM BY PYTHONDEX 
# Visit https://pythondex.com for more information

user = {
    'pin': 1234,
    'balance':1000
}

def widthdraw_cash():
    while True:
        amount = int(input("Enter the amount of money you want to widthdraw: "))
        if amount > user['balance']:
            print("You don't have sufficient balance to make this widthdrawal")
        else:
            user['balance'] = user['balance'] - amount
            print(f"{amount} Dollars successfully widthdrawn your remaining balance is {user['balance']} Dollars")
            print('')
            return False

def balance_enquiry():
    print(f"Total balance {user['balance']} Dollars")
    print('')


is_quit = False

print('')
print("Welcome to the Pythondex ATM")

pin = int(input('Please enter your four digit pin: '))     

if pin == user['pin']:
    while is_quit == False:
        print("what do you want to do")
        print(" Enter 1 to Widthdraw Cash \n Enter 2 for Balance Enquiry \n Enter 3 to Quit")
        
        query = int(input("Enter the number corresponding to the activity you want to do: "))
        
        if query == 1:
            widthdraw_cash()
        elif query == 2:
            balance_enquiry()
        elif query == 3:
            is_quit = True

        else:
            print("Please enter a correct value shown")
else:
    print("Entered wrong pin")

Above is the complete code for this atm program. I will explain each part of the code in a later part. First, let’s run this program. You should have python installed on your computer if you don’t have read this: Install and setup python or else you can use this online compiler.

Now you have the code and you are ready to run this program so open a command prompt or terminal at the project folder location and paste the below command to run.


python filename.py

The above command will run this program. It will welcome you and ask you to enter what you want to do below is an example output of this program.


Welcome to the Pythondex ATM
Please enter your four digit pin: 1234
what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 2
Total balance 1000 Dollars

what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 1000
Please enter a correct value shown
what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 2
Total balance 1000 Dollars

what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 1
Enter the amount of money you want to widthdraw: 1000
1000 Dollars successfully widthdrawn your remaining balance is 0 Dollars

what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 2
Total balance 0 Dollars

what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit
Enter the number corresponding to the activity you want to do: 1
Enter the amount of money you want to widthdraw: 1
You don't have sufficient balance to make this widthdrawal
Enter the amount of money you want to widthdraw: 0
0 Dollars successfully widthdrawn your remaining balance is 0 Dollars

what do you want to do
 Enter 1 to Widthdraw Cash
 Enter 2 for Balance Enquiry
 Enter 3 to Quit

If you have ran this program then you can see that we have successfully created an atm program that has the functionalities of an atm machine.

Python ATM Program Code Explanation.

We have seen how to run this program. Now let’s see and understand how this program works. I will go through each part of the program so you can understand it. You need to have the basic knowledge of python to understand it.

I have used a combination of while loops, functions and if else statements to create this program, so you need to know how they work.


user = {
    'pin': 1234,
    'balance':1000
}

First, I have declared a user dictionary which stores the pin and balance of the user account. User will need to enter this pin when they run this program and it will have default balance of 1000 dollars you can change it however you want.


print("Welcome to the Pythondex ATM")

pin = int(input('Please enter your four digit pin: '))     

Then we have a print statement which welcomes the user and a input where the user is asked to enter a 4 digit pin. You need to enter the pin which is hard-coded in the user dictionary.


if pin == user['pin']:
    while is_quit == False:
        print("what do you want to do")
        print(" Enter 1 to Widthdraw Cash \n Enter 2 for Balance Enquiry \n Enter 3 to Quit")
        
        query = int(input("Enter the number corresponding to the activity you want to do: "))
        
        if query == 1:
            widthdraw_cash()
        elif query == 2:
            balance_enquiry()
        elif query == 3:
            is_quit = True

        else:
            print("Please enter a correct value shown")
else:
    print("Entered wrong pin")
        

After entering the pin it checks the pin if it matches, If it does not match then it will print the wrong pin entered message in the else statement. If it matches, then it will go inside the if statement.

As you can see, inside the if statement we have while loop which will run until is_quit variable becomes True which is false by default.

Now it will ask the user what you want to do and the response the user should give are as follows:

  • Enter 1 to withdraw cash
  • Enter 2 for balance enquiry
  • Enter 3 to quit the program.

Then we check the response from the user in the if else statement and if it matches the response number it goes and runs the functions.


def widthdraw_cash():
    while True:
        amount = int(input("Enter the amount of money you want to widthdraw: "))
        if amount > user['balance']:
            print("You don't have sufficient balance to make this widthdrawal")
        else:
            user['balance'] = user['balance'] - amount
            print(f"{amount} Dollars successfully widthdrawn your remaining balance is {user['balance']} Dollars")
            print('')
            return False

Above is the withdraw money function, which will run if the query is 1. Inside the withdraw function it asks the user to enter the amount they want to withdraw and if the amount entered is available in their balance they will get the cash.

But if not then it will print an insufficient balance message and it will ask them to enter the amount to be withdrawn.


def balance_enquiry():
    print(f"Total balance {user['balance']} Dollars")
    print('')

Above is the balance enquiry function, which will run if the query matches 2 as input. This function will print the balance of the user.

Lastly we have another query which if the user enters 3 as input the atm program will quit.

Summary

This was the tutorial on creating an atm program using python. I hope you found this tutorial helpful and useful. Do share this with your friends who might be interested in this python program.

Here are some more python tutorials that will interest you:

I hope you found what you were looking for from this python tutorial and if you want more tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂