Bank Teller question

To update the balance you have to set 2 variables equal to a function. My question is why can’t you just call make_deposit since you already add the amount to the savings_balance. Why do you need to set savings_balance & checking_balance equal to the make_deposit function?

savings_balance, checking_balance = make_deposit(“savings”,10,checking_balance,savings_balance

#initial checking_account_balance
checking_balance = 0

#initial saving_account_balance
savings_balance = 0
acc_error = “Unsuccessful, please enter “checking” or “savings””

#check_balance definition
def check_balance(account_type, checking_balance, savings_balance):
if account_type == “savings”:
balance = savings_balance
elif account_type == “checking”:
balance = checking_balance
return acc_error
balance_statement = "Your " + account_type + " balance is " + str(balance) + “.\n”
return balance_statement

print(check_balance(“checking”, checking_balance, savings_balance))
print(check_balance(“savings”, checking_balance, savings_balance))

#deposit function
def make_deposit(account_type,amount,checking_balance,savings_balance):
deposit_status = “”
if amount > 0:
if account_type == “savings”:
** savings_balance += amount**
** deposit_status = “successful”**
elif account_type == “checking”:
checking_balance += amount
deposit_status = “successful”
deposit_status = acc_error
deposit_status = “Unsuccessful, please enter an amount greater than 0”

deposit_statement = "Deposit of "+ str(amount) + " dollars to your " + account_type + " account was " + deposit_status + "."

#print deposit statement

return savings_balance, checking_balance

#update bank account after savings deposit
savings_balance, checking_balance = make_deposit(“savings”,10,checking_balance,savings_balance)

#check savings balance call after deposit

#update bank account after checking deposit
savings_balance, checking_balance = make_deposit(“checking”,200,checking_balance,savings_balance)

#check checking balance call after deposit

Hi, @py1442802983, Welcome to the Codecademy Forums!

Excellent question — the reason has to do with global variables vs. local variables.

There is a quick answer to your question, as well as a longer discussion as to why changing global variables in your functions can have unintended consequences (and is generally inadvisable, especially to newer programmers). I’ll do my best to explain the former, but the latter has been covered many times elsewhere (here, for example).

Quick answer

The lines savings_balance += amount and checking_balance += amount are updating the local variables savings_balance and checking_balance rather than the global variables of the same names. As a reminder, local variables are variables contained only within your functions, and global variables are the variables that exist in your file outside of your functions.

How does this work?

Your make_deposit function takes the arguments checking_balance and saving_balance, which you set equal to the global amounts when you call your function using the following syntax:


Then the rest of your function references those arguments, and not the global variables.

One way to think of it is that your function is doing this:

  • Saving new local variables
    • (local)checking_balance = (global)checking_balance
    • (local)savings_balance = (global)savings_balance
  • Incrementing the local variable by the deposit amount
    • (local)savings_balance += amount
  • And returning the local variables
    • return (local)savings_balance, (local)checking_balance

The global savings_balance and checking_balance don’t actually get updated until you reassign them to the returned amounts from make_deposit:

savings_balance, checking_balance = make_deposit(“checking”,200,checking_balance,savings_balance)

You can see this if you just call your make_deposit function without the savings_balance, checking_balance = part and then run your check_balance function. Your balances will remain zeros.


If you really hate having to reassign those values each time you call your function, there are other ways to achieve a similar result (see, e.g., the python docs).

One workaround you may like is the use of a dictionary, which is mutable in place.
Since I don’t remember where exactly in the curriculum this exercise is, I’m not sure how familiar you are with dictionaries. However, check out how I reworked your code below and see if you like it better.

balances = {"checking": 0, "savings": 0}

def make_deposit(account_type, amount, balances):
    deposit_status = ""

    if amount > 0:

        if account_type == "savings":
            balances["savings"] += amount
            deposit_status = "successful"

        elif account_type == "checking":
            balances["checking"] += amount
            deposit_status = "successful"

            deposit_status = acc_error

        deposit_status = "Unsuccessful, please enter an amount greater than 0"

    deposit_statement = "Deposit of "+ str(amount) + " dollars to your " + account_type + " account was " + deposit_status + "."

make_deposit("checking", 200, balances)

I also struggled with this problem. Thank you so much for explaining it well