Python "return" difficulties -
first, let me have done thorough research trying understand. not understand explanations others have given (which understood asked question). code have problems with:
def tax(bill): """adds 8% tax restaurant bill.""" bill *= 1.08 print "with tax: %f" % bill return bill def tip(bill): """adds 15% tip restaurant bill.""" bill *= 1.15 print "with tip: %f" % bill return bill meal_cost = 100 meal_with_tax = tax(meal_cost) meal_with_tip = tip(meal_with_tax) when delete first "return bill" , run it, first number there error when tries calculate second number. def tax(bill) takes 100 , outputs 108 right? if delete first "return bill", why def tip(bill) not doing it's calculations 108 instead of giving error?
i have problem grasping basic concepts of programming. i've been learning intensely 3 months , am. slippery subject mind grasp , appreciate help.
i believe you're misunderstanding difference between return , print statement.
in tax(bill) function, you're multiplying bill *= 1.08. print value of bill. print outputs value of bill console window -- doesn't store value or allow used other functions in way.
the return statement you're deleting returns value stored in bill, 108, caller. caller in instance meal_with_tax = tax(meal_cost). means value of meal_with_tax return value of tax(bill). when delete return bill, you're returning value of none, value of meal_with_tax none. want return bill assign value 108 meal_with_tax.
the reason tip(bill) returns error because it's attempting calculate none *= 1.15. value of bill inside tip(bill) not 108 think.
Comments
Post a Comment