4. Transportation- Taking a Vacation


#1



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


It keeps saying...
Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (100) for 3 days.


I assume I am doing things correctly here. What is wrong with my code? Thanks.


Replace this line with your code.


#2

functions return None by default, so it indicates cost isn't returned when the car is rented for 3 days


#3

Just you have written function and nothing you print on console by using print command.
So Print value by print rental_car_cost(3) = 100


#4

I also got that error until I got rid of the "else:" line. If you write "else," you only return cost if you rent the car for less than three days, but we always want to return the cost.

The cost is calculated right away at the top (cost = 40 * days). Then a discount is applied if relevant. If no discount is relevant, there is no "else" thing you need to do. We want that return statement to apply always, not just if there were no discount. Hope that makes sense.

In other news, It looks like you also need to change your - to -=. This will be equivalent to the += shown in the example. (If not you will probably get the error "It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days.") We weren't directly taught what += and -+ mean, but it looks like it is shorthand for adding or subtracting to itself. So I think cost -= 20 means the same as cost = cost - 20.


#5

Thanks for your response. I understand what you mean, yet, I still have trouble solving my issue. How come 1 and 2 days can work but not 3 for my car rental function? What am I missing?


#6

Hi, thanks a lot for your detailed information. I adjusted the codes as below and it worked. However, one question, why do I not need to put "return" in if and elif statements in this rental_car_cost() function but I had to put return in my plane_ride_cost() function?

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


#7

because you chose a different design for this function.

back to the original code you posted in this topic, you could also have decided to return cost - 50 if the car is rented for 7 days or more

you chose to apply discount and then add the end return cost.


#9

Yes, in this case, we are looking for only one cost. First we calculate the base cost, then we check for possible discounts, then we return the final cost.


#10

i know, you don't have to tell me :wink:

You replied to me instead of crispy2013


#11

Thanks for both of your help.


#12

OK, I see. So I guess there may be no issue even if I want to return the number on each line.

As like,

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

In the end, I will still have to return the end cost. It may just be unnecessary to do that.


#13

you can't return and update a variable on the same line, you either return cost -50 or you apply the discount (updating variable) and then add the end return the cost, this are the two common ways to solve this problem


#14

Thanks bro, your answer helped a lot.


#15