'Functions & returning to previous code python

import random
import math

main_menu = [
    "1 - Display Balance",
    "2 - Withdraw Funds",
    "3 - Deposit Funds",
    "9 - Return Card",
]
print("Welcome to Northern Frock")
print(main_menu[0])
print(main_menu[1])
print(main_menu[2])
print(main_menu[3])

atm_input_1 = int(
    input(
        "Select 1 to display the current balance and the maximum amount available for withdrawal (In £10 increments) \n select 2 to view avalaible withdrawal amounts \n Select 3 to deposit funds \n select 9 to return card!"
    )
)
current_balance = random.randint(10, 1000)
withdrawal_balance = math.floor(current_balance / 10) * 10

if atm_input_1 == 1:

    print("Current Balance:", "£", current_balance)

    print("Withdrawal Balance:", "£", withdrawal_balance)

elif atm_input_1 == 2:
    sub_menu = [
        "1 - £10",
        "2 - £20",
        "3 - £40",
        "4 - £60",
        "5 - £80",
        "6 - £100",
        "7 - Other amount",
        "8 - Return to main menu",
    ]
    print("Please select withdrawal amount")
    print(sub_menu[0])
    print(sub_menu[1])
    print(sub_menu[2])
    print(sub_menu[3])
    print(sub_menu[4])
    print(sub_menu[5])
    print(sub_menu[6])
    print(sub_menu[7])

    sub_menu_input = int(input("Please select a number from the options below"))
    if sub_menu_input == 1:
        if withdrawal_balance >= 10:
            print("£10 successfully withdrawed from account")
            current_balance - 10
            withdrawal_balance - 10
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")
    elif sub_menu_input == 2:
        if withdrawal_balance >= 10:
            print("£20 successfully withdrawed from account")
            current_balance - 20
            withdrawal_balance - 20
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")

    elif sub_menu_input == 3:
        if withdrawal_balance >= 10:
            print("£40 successfully withdrawed from account")
            current_balance - 40
            withdrawal_balance - 40
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")

    elif sub_menu_input == 4:
        if withdrawal_balance >= 10:
            print("£60 successfully withdrawed from account")
            current_balance - 60
            withdrawal_balance - 60
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")

    elif sub_menu_input == 5:
        if withdrawal_balance >= 10:
            print("£80 successfully withdrawed from account")
            current_balance - 80
            withdrawal_balance - 80
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")

    elif sub_menu_input == 6:
        if withdrawal_balance >= 10:
            print("£100 successfully withdrawed from account")
            current_balance - 100
            withdrawal_balance - 100
            print("Current Balance:", "£", current_balance)
            print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("Sorry you currrently have insufficent funds for a withdrawal")

    elif sub_menu_input == 7:
        withdrawal_request = int(
            input("Please enter amount you wish to withdraw (In a £10 sequence")
        )
        if withdrawal_request % 10 == 0:
            if withdrawal_balance >= withdrawal_request:
                print("£", withdrawal_balance, "successfully withdrawed from account")
                current_balance - withdrawal_request
                withdrawal_balance - withdrawal_request
                print("Current Balance:", "£", current_balance)
                print("Withdrawal Balance:", "£", withdrawal_balance)

        else:
            print("ERROR Invalid withdrawal request")

elif atm_input_1 == 3:
    deposit_request = int(
        input("Please enter the amount you wish to deposit into your account")
    )
    current_balance = current_balance + deposit_request
    withdrawal_balance = math.floor((withdrawal_balance + deposit_request) / 10) * 10
    print("you have successfully deposited £", deposit_request, "into your account")
    print("updated balances:")
    print("Current Balance:", "£", current_balance)
    print("Withdrawal Balance:", "£", withdrawal_balance)

elif atm_input_1 == 9:
    print("Card returned , thank you for banking with Northern Frock good day")
    quit()

else:
    print("Error invalid selection try again")

2 questions How do i return to the main menu (the first user input)after withdrawing , depositing and displaying the balance 2)How could i write my program as a function or a series of functions instead of its currentform any help will be greatly appreciated in my development thank you :) currently the user is greeted with the first main menu (this is where i need the user to return >too after every ending instead of the program just stopping as im trying to have it work like an >actual atm where you have to select to leave



Solution 1:[1]

First, to have the program run until it is finished we want to run in a loop.

def main():
    print("Welcome to Northern Frock")
    account = {'balance': random.randint(10, 1000)}
    while not _main_menu(account):
        pass

Second, you can define functions that each perform some tasks and build your program piece by piece from these functions.

Selecting menu options. This function displays the option and returns the user selection.

def _get_input(options: list, keys=None):
    if keys is None:
        keys = list(range(1, len(options) + 1))

    msg = '\n'.join(f'{i} - {m}' for i, m in zip(keys, options))
    try:
        opt = int(input(msg))
        if opt in keys:
            return opt
        else:
            return -1
    except ValueError:
        print(f'Please select an option from the list')
        return -1

Custom input amount. Simple function to get a number for custom deposit and withdrawal.

def _custom_amount():
    try:
        amount = int(input('Please enter amount: '))
        if amount < 0:
            print('ERROR Please enter a number greater than 0')
            return 0
        else:
            return amount
    except ValueError:
        print('ERROR Please enter a number')
        return 0

Main menu functions that work on the account.

def _display_balance(account):
    print(f"Current Balance: £{account['balance']}")
    print(f"Withdrawal Balance: £{int(account['balance'] / 10) * 10}")

def _withdraw_funds(account):
    amounts = {1: 10, 2: 20, 3: 40, 4: 60, 5: 80, 6: 100, 7: _custom_amount}
    options = [
        "£10",
        "£20",
        "£40",
        "£60",
        "£80",
        "£100",
        "Other amount",
        "Return to main menu"
    ]

    selection = _get_input(options)
    while selection == -1:
        selection = _get_input(options)
    if selection in amounts:
        if selection < 7:
            amount = amounts[selection]
        else:
            amount = amounts[selection]()
        if not amount:
            return False
        available = int(account['balance'] / 10) * 10
        if amount % 10 or amount > available:
            print("ERROR Invalid withdrawal request")
        else:
            account['balance'] -= amount
            print(f"£{amount} successfully withdrawn from account")
            _display_balance(account)
    return False

def _deposit_funds(account):
    amounts = {1: 10, 2: 20, 3: 40, 4: 60, 5: 80, 6: 100, 7: _custom_amount}
    options = [
        "£10",
        "£20",
        "£40",
        "£60",
        "£80",
        "£100",
        "Other amount",
        "Return to main menu"
    ]

    selection = _get_input(options)
    while selection == -1:
        selection = _get_input(options)
    if selection in amounts:
        if selection < 7:
            amount = amounts[selection]
        else:
            amount = amounts[selection]()
        if not amount:
            return False
        account['balance'] += amount
        print(f"You have successfully deposited £{amount} into your account")
        _display_balance(account)
    return False

def _return_card(account):
    print("Card returned , thank you for banking with Northern Frock good day")
    return True

And the main menu function itself.

def _main_menu(account):
    callees = {1: _display_balance, 2: _withdraw_funds, 3: _deposit_funds, 9: _return_card}
    keys = [1, 2, 3, 9]
    options = [
        "Display Balance",
        "Withdraw Funds",
        "Deposit Funds",
        "Return Card"
    ]
    selection = _get_input(options, keys)
    while selection == -1:
        selection = _get_input(options, keys)
    return callees[selection](account)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MYousefi