Transportation (returning 120 and 100?)


#1


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

Stumped, I am not sure whats going wrong here.
Error message
Oops, try again. It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days.


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


#2

Let us assume that days = 3 (just like in the error message). This is what happens inside your function:

cost = 40 * 3 = 120
return cost => return 120

That's all. return statement terminates the function. Do you really need this line?


#3

Thank you for responding back! However after removing return statement, I am now getting this error message.
Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.
So my question to you is why?
code below

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


#4

Now only a return keyword is reached when you rent a car 3 or more days (and 7 more days), not when you rent the car for 1 or 2 days

Keep in mind that a return keyword causes a function to end, so you should place return in a good place so that it also returns the cost for renting a car 1 or 2 days, but not blocking for renting a car 3 or more days


#5

Thank you for the quick response! I really appreciate it. Unfortunately, removing return keyword is still resulting in the same error message.
Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -50
elif days >= 3 and days <= 6:
cost - 20
My question why am I receiving the same error message?


#6

from this code:

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

you should not have removed return keywords, you needed to add a return keyword so that cost is also returned if i want to rent a car for one or two days


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.