ERROR It looks like rental_car_cost returns -10 instead of the correct amount (40) for 1 days


#1



It looks like rental_car_cost returns -10 instead of the correct amount (40) for 1 days.
Please help me guys i spend 1 hour figure out what is the problem.


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


#2

The indent level for your return statement should be on the same level as your if statement.

And your code can be shortened a little bit by only returning cost once. Also, this line

You can write it like this, cost -= 50


#3

Can you attach a link to the exercise, and I will try my best to help you


#4

Not necessarily, he can also return variables inside a conditional.


#5

The error he's getting is because of the return statement's indent level. Test it for yourself if you think I'm wrong. :slight_smile:


#6

if i rent the car for one day:

cost = (days) * 40

so that is 1 * 40 = 40.
then the if condition:

if (days) <= 7:
        cost = cost - 50
        return cost

is 1 less or equal to 7? Yes it is, so 40 - 50 = -10, which gets returned, end of function.

I would check if the car gets rented for less then 3 days, and return the cost without discount if it is. You might want to check the order of the if/elif statements as well.

the exercise url: https://www.codecademy.com/en/courses/python-beginner-P5YtY/1/3

@bandit, you can return in each if/elif/else statement, that is fine. It is more code then just updating the cost variable and returning it at the end, but both are perfectly fine.


#7



Like this? but give the error It looks like rental_car_cost returns 10 instead of the correct amount (40) for 1 days.


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


#8

You still have indented properly and done what @stetim94 said.


#9

no, that made it even worse. you should add another if statement to check if the car is rented less then 3 days, in which case cost should be returned without discount.

also, if this is true:

if (days) <= 7:

this can impossible run:

elif (days) <= 3:

since everything smaller then 7 is also smaller then 3. Less then 3 days also shouldn't give any discount. 3 till 6 days should give 20 discount, 7 or more should give 50 discount.


#10



mhm and now?


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


#11

this:

cost = 50 - 40

is wrong, it should be the way it was:

cost = days - 50

this:

elif (days) <= 3

should be 3 days or moer:

elif (days) >= 3

and you should still return cost without any discount if it is less then 3 days (3 not included), you can add an else


#12



now i have another error on else and i cant figure out syntax error.


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


#13

The else statement in Python doesn't require a clause. The Syntax is

else:

#14

Think about the logic of this for a second. The instructions clearly state that if the amount of days is bigger than or equal to 7, there is a discount of $50 (cost - 50), and if it is greater then or equal to 3, then it should only be a $20 discount (cost -20) and otherwise it should just stay the same. Think about the instruction, and then implement them in your code

Hope I helped,
Barry Allen


#16

A post was split to a new topic: Rental car


#17