Does return finishes a function on Python? -


i'm learning on codeacademy , trying print following:

def trip_cost(p, d, m):     return rental_car_cost(d) + hotel_cost(d) + plane_ride_cost(p) + m     print "%s %s %s days %s dollars of spending mone" (trip_cost(p, d, m), p, d, m) 

the program telling me nothing printing on console, proceeded delete return line , worked, wondering if every time function reaches return finishes, in case in following code could've save "and days < 7" ??

def rental_car_cost(days):           cost = days*40      if days >= 7:         cost -= 50         return cost         elif days >= 3 , days < 7:         cost -= 20         return cost     print cost         

so wondering if every time function reaches return finishes

yes, returns called it, hence name.

in case in following code could've save and days < 7

yes. have left out because elif means "else if" if didn't return case have been considered if previous if had been false considered else.

generally call code and days < 7 here redundant. it's idea remove redundant code. if redundancy makes clearer there's little harm leaving in, rule redundancy more confuse assist them, when more familiar language.


Comments