ATM Lesson 2: Menu Loop

Introduction

Now that we have the basic functions in our ATM, we will make a basic loop of the program so that we can keep doing

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

Creating the prompt() function

Let us define the prompt for making another transaction:
def prompt():
    prompt = input("Would you like to make another transaction? (y/n) \n>>>")
    if prompt is "y":
        menu()
    elif prompt is "n":
        pass
    else:
        prompt()
Like before, we make a new variable called “prompt”, include the choices in the input (y/n), and then add a string of “>>>”. If the user inputs “y”, then we simply repeat the menu. If the user inputs “n”, then we “pass” and end the code. Any other input will repeat the prompt text.

Calling the prompt() fnction

To call the prompt function, simply add it to the last line of the menu function that we have.
def menu():
    print("Deposit - d")
    print("Withdraw - w")
    print("Check Balance - c")
    selection = input("d/w/c?\n>>> ")
    if selection is "d":
        deposit()
    elif selection is "w":
        withdraw()
    elif selection is "c":
        checkBalance()
    else:
        menu()
    prompt()    #Initializes the prompt() function

The Complete Code

myMoney = 0

def checkBalance():
    print("Your balance is: $ {} ".format(myMoney))
    
def deposit():
    global myMoney 
    depositAmount = input("How much would you like to deposit ($)? ")
    myMoney += int(depositAmount)
    print("Your new balance is ${}.".format(myMoney))
    
def withdraw():
    global myMoney
    withdrawAmount = input("How much would you like to withdraw ($)? ")
    myMoney -= int(withdrawAmount)
    print("Your new balance is ${}.".format(myMoney))

def prompt():
    prompt = input("Would you like to make another transaction? (y/n) \n>>>")
    if prompt is "y":
        menu()
    elif prompt is "n":
        pass
    else:
        prompt()
        
def menu():
    print("Deposit - d")
    print("Withdraw - w")
    print("Check Balance - c")
    selection = input("d/w/c?\n>>> ")
    if selection is "d":
        deposit()
    elif selection is "w":
        withdraw()
    elif selection is "c":
        checkBalance()
    else:
        menu()
    prompt()    #Initializes the prompt() function
    
menu()

This code will now loop back to itself. On the next lesson, we will add some error detection and correction to our code.

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account