Cash machine

A PIN with three tries, then balance, withdraw and deposit. Teaches text input, isdigit, len and menus.

Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.

Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.

Flowchart

Flowchart of the Cash machine example

Python program

The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.

"""An ATM with a PIN, withdrawals and deposits. Teaches program state and input checks."""
pin: str = "0420"
entered: str
tries: int = 0
ok: bool = False
# Three tries for the PIN, then the card is blocked.
while not ok and tries < 3:
    entered = input("PIN (4 digits):")
    tries = tries + 1
    if len(entered) == 4 and entered.isdigit():
        if entered == pin:
            ok = True
        else:
            print("Wrong PIN.")
    else:
        print("The PIN has exactly 4 digits.")
if ok:
    balance: int = 1000
    option: str = ""
    while option != "4":
        print("")
        print("1 Balance")
        print("2 Withdraw")
        print("3 Deposit")
        print("4 Quit")
        option = input("Choice:")
        if option == "1":
            print("Your balance is", balance, "CZK")
        else:
            if option == "2":
                amount: int
                amount = int(input("How much to withdraw:"))
                if amount <= 0:
                    print("Enter a positive amount.")
                else:
                    # You cannot withdraw more than the balance.
                    if amount > balance:
                        print("Not enough money in the account.")
                    else:
                        balance = balance - amount
                        print("Paid out", amount, "CZK")
            else:
                if option == "3":
                    deposit: int
                    deposit = int(input("How much to deposit:"))
                    if deposit <= 0:
                        print("Enter a positive amount.")
                    else:
                        balance = balance + deposit
                        print("Deposited", deposit, "CZK")
                else:
                    if option != "4":
                        print("Unknown choice.")
    print("Goodbye.")
else:
    print("The card is blocked.")

Follow-up tasks

Tasks in which you change the example (with a model solution):

All examples