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()
您需要将
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()