首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数&返回到以前的代码python

函数&返回到以前的代码python
EN

Stack Overflow用户
提问于 2022-04-28 17:03:25
回答 1查看 34关注 0票数 -1
代码语言:javascript
复制
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问题如何在退出、保存和显示余额2之后返回主菜单(第一个用户输入)如何将程序编写为函数或一系列函数,而不是当前的形式--在我的开发过程中,任何帮助都将受到极大的赞赏,谢谢:)目前,用户受到第一个主菜单的欢迎(我需要用户在每次结束后也返回>,而不是在程序结束后停止工作,就像我不得不选择离开

的>实际atm一样)。

EN

回答 1

Stack Overflow用户

发布于 2022-04-28 18:26:10

首先,要让程序运行到完成,我们需要在一个循环中运行。

代码语言:javascript
复制
def main():
    print("Welcome to Northern Frock")
    account = {'balance': random.randint(10, 1000)}
    while not _main_menu(account):
        pass

其次,您可以定义每个函数都执行一些任务,并从这些函数逐个构建程序。

选择菜单选项。此函数显示该选项并返回用户选择。

代码语言:javascript
复制
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

自定义输入量。简单的功能,得到一个数字的定制存款和取款。

代码语言:javascript
复制
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

主菜单功能,在帐户上工作。

代码语言:javascript
复制
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

以及主菜单功能本身。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72047650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档