每当我使用存款方式然后检查余额方式时,余额都不会更新任何修复吗?

问题描述 投票:0回答:1
balance = 0
def display_menu():
    balance = 0
    choice = ' '
    if choice == ' ':
        print('1.Check Balance \n2.Deposit \n3.Withdraw \n4.Exit')
        choice = int(input('Please make a choice: '))
        if choice == 1:
            check_balance()
            display_menu()
        elif choice == 2:
            balance = deposit(balance)
            display_menu()
        elif choice == 3:
            balance = withdraw(balance)
            display_menu()
        elif choice == 4:
            print('Thank you have a good day')
        
def deposit(balance):
    deposit_amount = float(input('Please enter the amount you wish to deposit'))
    balance += deposit_amount
    print(f'The updated balance is: {balance:.2f}')
    return balance 
      
def check_balance():
    print(f'Balance is: {balance:.2f}')
       
display_menu()
python return
1个回答
0
投票

您需要将

balance = 0
函数中的
display_menu()
替换为
global balance

balance = 0
def display_menu():
    global balance 
    choice = ' '
    if choice == ' ':
        print('1.Check Balance \n2.Deposit \n3.Withdraw \n4.Exit')
        choice = int(input('Please make a choice: '))
        if choice == 1:
            check_balance()
            display_menu()
        elif choice == 2:
            balance = deposit(balance)
            display_menu()
        elif choice == 3:
            balance = withdraw(balance)
            display_menu()
        elif choice == 4:
            print('Thank you have a good day')
        
def deposit(balance):
    deposit_amount = float(input('Please enter the amount you wish to deposit'))
    balance += deposit_amount
    print(f'The updated balance is: {balance:.2f}')
    return balance 
      
def check_balance():
    print(f'Balance is: {balance:.2f}')
       
display_menu()
© www.soinside.com 2019 - 2024. All rights reserved.