2 回答

TA貢獻1804條經(jīng)驗 獲得超7個贊
您應(yīng)該return在代碼中使用 a ,然后將其分配給您的變量。這將是您的代碼:
balance = 500
def main(balance):
if balance <= 999999:
intrest = 0.01
if balance >= 1000000:
intrest = 0.02
menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
if menu == 1:
print(balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
balance = (balance + dep)
print('Your new balance is', balance)
if balance <= 999999:
intrest = 0.01
if balance >= 1000000:
intrest = 0.02
print('Congratulations, your intrest just went up!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
balance = (balance - wit)
print('Your new balance is', balance)
if balance <= 999999:
intrest = 0.01
print('Your intrest is now back to standard!')
elif menu == 4:
intrest_return = balance + (balance * intrest)
print(intrest_return)
while True:
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'y':
main(balance)
else:
break
return balance
balance=main(balance)
print(balance)

TA貢獻1828條經(jīng)驗 獲得超13個贊
添加global balance
函數(shù)的第一行。
除非您這樣做,否則在函數(shù)內(nèi)分配的任何變量都被視為本地變量,這就是您收到錯誤的原因。
添加回答
舉報