Returning -10 when I put abs(values)


#1

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?


#2

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


#3

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


#4

+40 is positve 40, not negative?


#5

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

rental_car_cost(1)
days = 1
Then
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.


#6

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


#7

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('> ')))) -_-


#8

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


#9

It should initialize a cost variable to operate on.


#10

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.


#11

Thank you so much, this makes much more sense now