I just completed the bank_teller Project, I used the solution set whenever I got stuck. My main sticking point was step 6 or anything similar to it it reads:
6. Call deposit function and make a savings deposit
On the next line, call the
make_deposit() function with these arguments;
savings_balance . Assign the function call to the matching
savings_balance variables that are also being returned by the function. That is how the new balances are being updated.
An acceptable line of code or at least one that was in the solution was:
savings_balance, checking_balance = make_deposit(“savings”, 10, checking_balance, savings_balance)
Truth be told I don’t exactly understand what is actually happening when I assign two variables to a function when two of those variables are parameters in the function. Can someone who is familiar with the project explain why this line of code is necessary? Or better yet what am I asking the computer to do when I assign two var. to a function that uses those variables as parameters?
It’s a bit of a confusing sentence but it seems like one way of trying to always keep values up to date.
The right hand side of any assignment will always be evaluated first so it doesn’t matter what happens to variables in this case. A simplified version of this would be something like-
my_money = 1
my_money = my_money + 1
The calculation of
my_money + 1 is performed first and only once it has completed that new integer is then assigned to the name
my_money. This is not different for a function,
a = func(a). For example passing
savings_balance to a function means that function is evaluated first. That function returns a value when it finishes execution. Reassigning that output to
savings_balance is a way to update it.
I’m assuming your function is written to update a variable and return it? You must then reassign it or your updated value will be forgotten.
Here’s an example of why you would need to reassign (assuming your function is defined like this)-
def add_to_savings(amount_added, savings):
savings += amount_added
tom_savings = 100 # tom has 100 in savings
claire_savings = 200 # claire has 200 in savings
# we add to tom_savings and reassign it
tom_savings = add_to_savings(10, tom_savings)
# we add to claire_savings but don't reassign
Out: 110 # tom_savings has been added to
Out: 200 # claire_savings has not (not good!)
If that’s not how your function is set-up then you’d need to provide some more details. There’s nothing different in how this would work for two variables.
Projects are often a bit more advanced than lessons but it is important you understand any code you use within them. If you find yourself using solutions make sure you understand exactly why an answer would be used as gaps in your knowledge will only compound with further lessons. Don’t be afraid to redo lessons if necessary until you are sure you’ve grasped the concept.