Newbie confused about the return functions

Hi all,

I am working through an off-platform Bank Teller project and I am confused on one particular concept.

One small change to the code results in incorrect output, and I am wondering how the omission affects the logic.

Apologies in advance if this is the wrong place to post.

Correct answer should be:

#Block A initial checking_account_balance and savings_account_balance
checking_balance = 200
savings_balance = 10
#Block B 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
    else:
        return acc_error
    balance_statement = "Your " + account_type + " balance is " + str(balance)
    return balance_statement
#Block C check checking and savings balance call
print(check_balance("checking",checking_balance,savings_balance))
print(check_balance("savings",checking_balance,savings_balance))
#Block D withdrawal function
def make_withdrawal(account_type,amount,checking_balance,savings_balance):
    withdrawal_status = ""
    fail = "unsuccessful, please enter amount less than balance"
    if account_type == "savings":
        if amount <= savings_balance:
            savings_balance -= amount
            withdrawal_status = "successful"
        else:
            withdrawal_status = fail
    elif account_type == "checking":
        if amount <= checking_balance:
            checking_balance -= amount
            withdrawal_status = "successful"
        else:
            withdrawal_status = fail
    else:
        withdrawal_status = acc_error
    
    withdrawal_statement = "Withdrawal of "+ str(amount) + " from your " + account_type + " account was " + withdrawal_status + "."

    #print withdrawal statement
    print(withdrawal_statement)  
    return savings_balance, checking_balance
#Block E update bank account after checking withdrawal
savings_balance, checking_balance = make_withdrawal("checking",170,checking_balance,savings_balance)
#Block F check checking balance call after withdrawal
print(check_balance("checking",checking_balance,savings_balance))

=====================================

The only difference I had in my coding is at Block E, I failed to include
savings_balance, checking_balace = … the correct ending balance should be 30, my incorrect result shows 200

My logic being I already had a return savings_balance, checking_balance in Block D which registered the running total, so block F should reflect the correct balance.

I have a feeling I might misunderstood what “return” does in general. Super grateful if someone can shed some light!

return is literally what is says, returning/handing back data from the function

here:

def make_withdrawal(account_type,amount,checking_balance,savings_balance):

we have now two checking_balance variables:
the one you defined in block A (global scope)
the parameter of the make_withdrawal function (local scope)

within the body of the function the parameter/locally scoped variable are used. What might be confusing here is that we can have the same variable names multiple times

which is why block E is so crucial to update the global variables.

2 Likes

Hi! Thanks so much for your explanation. It makes a lot of sense.

So great to have this forum here. Otherwise it might take me years to this realization!

2 Likes