Transportation 4: Variable use


A cool, easier way to do this lesson that I thought of, by using a variable. I didn't see many people doing it, so I thought I'd share this idea.

Kind of like this:

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


I did that since i was following the example in the tutorial. It doesn't seem to work for me for some reason

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

I just noticed that your last line of code has says: return:price, that worked for me, Could you explain why that last line is necessary?



The problem in your code is that you need to put parenthesis around (days >= 7) and the same for the elif statement.

It doesn't seem to be a part of the instructions to add the else statement, I just did it to cover the scenarios in which the car is being rented for less than three days, when there is no discount at all. It also looks better to me with an ending else statement.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.