Python Tutorial: How to Build Your First ATM Terminal

Python Tutorial: How to Build Your First ATM Terminal

Python is quite a resourceful language for anyone hoping to build incredible solutions in AI, FinTech, automation and so on.

You’ll find this tutorial impactful if you are a beginner or intermediate Pythonista. Basically, you will learn how to build an ATM terminal that works locally.

For maximal learning, ensure you code along and understand every line you write; that’s how to keep thickening your software engineering experience.

Step-by-step guide to Building Your First ATM Console with Python

This is the simple logic of the program:

  • customers should be able to create ATM pins the pins should be of 4 digits and digits only

  • customers should be able to login only with their created pin and nothing else

  • customers should be able to:

    • see their balances

    • withdraw

    • deposit

    • exit

Step 1: Creating ATM Pins

Authentication is crucial to any financial software. As a result, this program will demand customers or bank users to create ATM pins they will subsequently need to access their bank accounts.

def create_pin():
    while True:
        print("*******************************")
        pin = input("Create a 4-digit PIN: ")
        print("*******************************")
        #@dev python doesn't support ||
        if pin.isdigit() and len(pin) == 4:
            print("*******************************")
            print("PIN successfully created!")
            print("*******************************")
            return pin
        print("*******************************")
        print("Invalid PIN; enter exactly 4 digits.")
        print("*******************************")

Here, we created the pin variable; ensured the length of the pin is not more than 4 with len(pin) == 4 and that it is only in digits [and not alphanumeric] with .isdigit.

Recall that Python does not support ||, hence the reason for using and.

Step 2: Accessing Bank Account with Created Pins

Once customers have created their pins once, they must use the same pin to access their accounts.

def login(pin):
    password_inputing_chances = 3
    while password_inputing_chances != 0:
        print("*******************************")
        entered_pin = input("Enter your 4-digit PIN: ")
        print("*******************************")
        if entered_pin == pin:
            print("Login successful!")
            return True
        # upon every attempt, there is one reduction
        password_inputing_chances -= 1
        print(f"{password_inputing_chances} attempts left")
    print("Account locked due to too many failed attempts.")
    return False

We created a function that takes in pin as a parameter, and remember that pin has been declared earlier.

It is the case that customers only have 3 attempts to input their pins, after which they will be automatically bounced out of the ATM console.

Customers can input their pins while their password_inputing_chances is not zero. Meanwhile, they have 3 chances to input the correct pin, or else, they will be bounced out of the console.

The number of attempts is tracked with password_inputing_chances -= 1. Therefore, customers lose a chance out of their 3 chances when they input incorrect pins.

Step 3: Check Balance Function

def show_user_balance(balance):
    print(f"Your balance is ${balance}")

This is a pretty straightforward code that prints customer balance. By the way, balance will be defined more properly later in the program.

Step 4: Deposit Function

def deposit():
    amount = float(input("Input the amount you want to deposit: $"))
    if amount <= 0:
        print("No, you can't deposit an insignificant amount.")
        return 0
    return amount

In the deposit function above, we ensured that customers cannot deposit amounts less than or equal to 0 with if amount <= 0.

Step 5: Withdrawal Function

def withdraw_money(balance):
    amount = float(input("Enter the amount of money you want to withdraw: $"))
    if amount > balance:
        print("You don't have sufficient funds for this.")
        return 0
    elif amount <= 0:
        print("You can only withdraw more than $0.")
        return 0
    return amount

We created two important statements here:

  • customer cannot withdraw greater than their balances

  • they cannot withdraw 0 or anything less than it

Step 6: Main Function for Logic Definition

def main():
    print("Welcome to Jubilee Bank!")
    pin = create_pin()

    if not login(pin):
        return

    balance = 0
    customer_engagement_is_running = True 

    while customer_engagement_is_running:
        print("\\n****************************")
        print("1. Show Balance")
        print("2. Deposit")
        print("3. Withdraw")
        print("4. Exit")
        print("****************************")

        choice = input("Please enter your choice between 1 and 4: ")

        if choice == "1":
            show_user_balance(balance)
        elif choice == "2":
            balance += deposit()
            print("Deposit successful.")
        elif choice == "3":
            balance -= withdraw_money(balance)
            print("Withdrawal successful.")
        elif choice == "4":
            customer_engagement_is_running = False
        else:
            print("Invalid choice, please pick between 1 and 4.")

    print("Thank you for banking with us!")

# this makes our script run as the "main" program
if __name__ == "__main__":
    main()

The first thing we did in the main function is to request customers create pin and only login with what they have created:

    if not login(pin):
        return

We set the global balance to 0.

Then we created a boolean variable called customer_engagement_is_running, such that when a customer is interacting with the ATM console, the logic will execute.

Customers have 4 choices:

  • know balance

  • withdraw

  • deposit

  • exit

We made the loop switch off whenever a customer wants to exit. With regards to deposit and withdrawal, we used -= and += to handle the decrementation and incrementation logic accordingly.

Finally, we included if __name__ == "__main__": main() so our main function can successfully execute.

Full Code

Here is the full code:

# ATM terminal Python program

# code logic:

# 1. customers should be able to create ATM pins 
#     the pins should be of 4 digits and digits only
# 2. customers should be able to login only with their created pin and nothing else 

# 3. customers should be able to: 

#     * see their balances
#     * withdraw
#     * deposit
#     *exit

# 4. we should use functions to make our code more readable
def create_pin():
    while True:
        print("*******************************")
        pin = input("Create a 4-digit PIN: ")
        print("*******************************")
        #@dev python doesn't support ||
        if pin.isdigit() and len(pin) == 4:
            print("*******************************")
            print("PIN successfully created!")
            print("*******************************")
            return pin
        print("*******************************")
        print("Invalid PIN; enter exactly 4 digits.")
        print("*******************************")

def login(pin):
    password_inputing_chances = 3
    while password_inputing_chances != 0:
        print("*******************************")
        entered_pin = input("Enter your 4-digit PIN: ")
        print("*******************************")
        if entered_pin == pin:
            print("Login successful!")
            return True
        # upon every attempt, there is one reduction
        password_inputing_chances -= 1
        print(f"{password_inputing_chances} attempts left")
    print("Account locked due to too many failed attempts.")
    return False

def show_user_balance(balance):
    print(f"Your balance is ${balance}")

def deposit():
    amount = float(input("Input the amount you want to deposit: $"))
    if amount <= 0:
        print("No, you can't deposit an insignificant amount.")
        return 0
    return amount

def withdraw_money(balance):
    amount = float(input("Enter the amount of money you want to withdraw: $"))
    if amount > balance:
        print("You don't have sufficient funds for this.")
        return 0
    elif amount <= 0:
        print("You can only withdraw more than $0.")
        return 0
    return amount

def main():
    print("Welcome to Jubilee Bank!")
    pin = create_pin()

    if not login(pin):
        return

    balance = 0
    customer_engagement_is_running = True 

    while customer_engagement_is_running:
        print("\\n****************************")
        print("1. Show Balance")
        print("2. Deposit")
        print("3. Withdraw")
        print("4. Exit")
        print("****************************")

        choice = input("Please enter your choice between 1 and 4: ")

        if choice == "1":
            show_user_balance(balance)
        elif choice == "2":
            balance += deposit()
            print("Deposit successful.")
        elif choice == "3":
            balance -= withdraw_money(balance)
            print("Withdrawal successful.")
        elif choice == "4":
            customer_engagement_is_running = False
        else:
            print("Invalid choice, please pick between 1 and 4.")

    print("Thank you for banking with us!")

# this makes our script run as the "main" program
if __name__ == "__main__":
    main()

Testing

You can easily test the code in your terminal by writing this on your terminal:

python name.py

If you play around with it, you will have something like this:

If you enjoyed this tutorial, I’ll encourage you to follow me on Twitter!