[Tutor] [maryknauth at mac.com: Re: Input parameters for functions]

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jun 20 13:57:14 EDT 2020


On 20/06/2020 18:22, Mary Knauth via Tutor wrote:

> I’ve updated the functions to reflect return account_balance,> but when I run other transactions the balance still defaults to
original $500.25

I suspect that's because you are not assigning the returned value.
It is not enough to return the value you have to use it in your
calling code. Although your  parameter has the same name as the variable
in your main code that is purely coincidental. The parameter inside the
function has no impact on the variable outside. You need to assign the
function return value to the variable.

account_balance = 500.00
amount = float(input("How much to deposit?"))
account_balance = deposit(account_balance, amount)  # assign new balance
print(account_balance)                 # should now be 500+amount

> def deposit(account_balance, deposit_amount):
>   deposit_amount = float(input("How much would you like to deposit today?\n"))
>   account_balance += deposit_amount                                           
>   print("Deposit was $%.2f , your new current balance is $%.2f" % (deposit_amount, account_balance))
>   return account_balance                                                       

Incidentally, it's considered best practice to avoid putting
calculations and input/output statements in the same function.
That's because it limits the ability to use the function in an
application that uses a different type of display - like a web app,
or GUI, say.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p2
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list