5. Pull it Together


#1

https://www.codecademy.com/courses/learn-python/lessons/taking-a-vacation/exercises/pull-it-together-?action=lesson_resume

I don’t understand why it prints this error message: “rental_car_cost(1) returned None instead of the correct value 40”



def answer():
  return 42

def hotel_cost(nights):
  cost = 140 * nights
  return cost

def plane_ride_cost(city):
  if city == "Charlotte":
    return 183
  elif city == "Tampa":
    return 220
  elif city == "Pittsburgh":
    return 222
  elif city == "Los Angeles":
    return 475
  
def rental_car_cost(days):
  cost = days * 40
  if days >= 7:
    return cost - 50
  elif days >= 3:
    return cost - 20

def trip_cost(city, days):
  return hotel_cost(a) + plane_ride_cost(b) + rental_car_cost(c)



#2

lets find out:

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

# lets rent the car for a day
print rental_car_cost(1)

prints None, None is what function return by default

which tells us, cost isn’t returned when the car is rented one day

according to you, which return keyword is reached when the car is rented for one day?


#3

I think it should be

return days * 40

#4

I finally did it, thank you


#5

you currently don’t have this code? adding it is a possibility, but make sure to place it right. A function ends the moment a return keyword is reached


#6

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