Transportation 4: Variable use


#1



https://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096


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
    else:
        return price


#2

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?

Thanks


#3

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.


#4

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