Transportation: 'returns 120 instead of the correct amount(100) for 3 days'


#1



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

"Oops, try again. It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days. " I keep getting the same error over and over again, even if I change the number 3 to 50 and 20 to 100! I thought it was a bug thus I tried on another computer but no difference.


My code looks perfect to me, it should give out 100. Please help me see what's wrong with it.


Replace this line with your code. 
def rental_car_cost(days):
    if days <= 3:
        cost = days * 40
        return cost
    elif days >= 3:
        cost -= 20
        return cost
    elif days >= 7: 
        cost -= 50
        return cost


#2

if the car gets rented for 3 days, you should give 20$ discount

but your if statement:

if days <= 3:

include 3, use < so 3 is not included.

this line also gives a problem:

cost -= 20

it is shorthand for:

cost = cost - 20

but if elif is true, if hasn't run. So cost is unassigned.


#3

OK,

now I have changed that part by:

elif days > 3:
cost = days * 40 - 20
return cost

I still get the same error, what is wrong?


#4

now 3 days doesn't give a 20$ discount. the elif >= 3 was fine, your if condition was wrong

if if is true, elif won't run. And if does include 3, while it shouldn't


#5

Yes now I understand what you mean! it was confusing for me because English is not my native language :slight_smile:
I changed it to this:
def rental_car_cost(days):
if days < 3:
cost = days * 40
return cost
if days >= 3 and days <= 6:
cost = days * 40 - 20
return cost
if days >= 7:
cost = days * 40 - 50
return cost

Now it works!