Taking a vacation...again

#1

Hi there!

Why isnt it possible to put return in front of cost -= 50?

(I know from the Q/A that it has to be at the bottom of the line break)

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

#2

Because we can only return an expression, not a statement. To return a statement, we need to put it in a function. For this solution we only need to subtract a number from the return value.

`````` return cost - 50

return cost - 20

return cost``````

#3

#4

There is a programming concept known as function factory. More formally it is referred to as closure.

``````def f(x):
def g(y):
return y - x
return g

d_max = f(50)
d_min = f(20)

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
return d_max(cost)
elif days >= 3:
return d_min(cost)
else:
return cost``````
`````` > rental_car_cost(10)
=> 350
> rental_car_cost(3)
=> 100``````

Notice that both `d_max()` and `d_min()` are clones of `g()` which is in the closure of `f()`? The closure instance variable, `x` is preserved on exit from the function. We return a function with this value as a constant.

#5

A post was split to a new topic: Checked it over and over and I don't know what is wrong

#6