Returning -10 when I put abs(values)


Here is my code for Python "Taking a Vacation" Exercise 4:
def rental_car_cost(days):
days = 40 * abs(days)
if days >= 7:
return days - 50
elif days >= 3:
return days - 30
elif days == 1:
return abs(40)
return days

but the error message states "Oops, try again. It looks like rental_car_cost returns -10 instead of the correct amount (40) for 1 days."
Am I missing something, or is there a bug?


Why do you return the absolute value of 40? Just add forty to days.


Because the error message kept telling me that the return was -10 when the input was 1, so I waned to make sure that the code itself didn't return a negative number


+40 is positve 40, not negative?


I see.
You're adding 40 to days, not to "cost"
See, the first line of your code multiplies by 40 whatever you put in for days - so

days = 1
days = 40 * days
(Days is now 40)
Since Days >= 7...
return Days (40) - 50
days = -10

you need a separate variable to track the cost.


The problem here is that he needs to be operating on a cost variable not on days, but he'll understand soon.


There's is a way to do it with one variable, but starting out with explicit logic is better. I still get tripped up on trying to return nested functions.

return(do_this(do_that(do_the_other(raw_input('> ')))) -_-


So if I don't want to try to make the function one line, it requires two variables?


It should initialize a cost variable to operate on.


It's better practice to have variables that make sense as you read them.
change your days = days * 40 to cost = days *40, and anywhere else you want to return the cost of rental for printing and you'll be fine.


Thank you so much, this makes much more sense now