Reputation: 23
I have a bank program where you can do multiple things, and the var balance needs to update based on user input. It does so the first time, and uses the new value in the next run. But if you run the loop a third time, it uses the balance output for the first run, and not the second. And if you run it a fourt time, it still uses the first value, and so on.
I am still very new to programming, but my theory is that the second loop cant return the new value of balance, so it just gets stuck on the first. But I don’t know how to work around that.
Does anyone here have any ideas? Thank you.
balance = 500
def main(balance):
menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
balance = (balance + dep)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
balance = (balance - wit)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if balance <= 999999:
interest = 0.01
if balance >= 1000000:
interest = 0.02
interest_return = (balance * interest)
balance = (balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new balance', balance)
return balance
balance = main(balance)
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
main(balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue
Upvotes: 0
Views: 2076
Reputation: 545
tldr;
balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
balance = main(balance) ########### HERE
else:
print()
print('Invalid input, press y for yes or n for no')
continue
explanation: in your original code you were assagning new value in every loop, so
balance(main)
was always starting of assigned value, and never changing it, because in the scope of main function changes was added to local balance, while outside of function you had global balance. Function was making changes to local, returning and printing local, while global was always same. to better understand that, try not to name local and global variables alike for example:
def main(local_balance):
menu = int(input('Press 1 for local_balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(local_balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
local_balance = (local_balance + dep)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
local_balance = (local_balance - wit)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if local_balance <= 999999:
interest = 0.01
if local_balance >= 1000000:
interest = 0.02
interest_return = (local_balance * interest)
local_balance = (local_balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new local_balance', local_balance)
return local_balance
global_balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
global_balance = main(global_balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue
Upvotes: 1
Reputation: 567
You need to update the balance
variable in the while loop with user input. Update the line main(balance)
to balance = main(balance)
Upvotes: 1