Besoin de votre aide


#1

Bonjour,
je ne sais pas ce qui ne va pas avec ce code, pouvez vous m’aider à résoudre ce problème

def hotel_cost(days):
  return 140*days

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 = 40 * days
  if days>=7:
    cost -= 50
    return cost
  elif days>=3:
      cost-=20
      return cost
    
def trip_cost(city ,days):
  return 140*days + city + cost

#2

In order to help you, we are going to need two things:

  • A link to the lesson
  • Any and all error messages you are receiving

#3

voila le lien pour la leçon
leçon
ainsi que le message :

rental_car_cost(1) returned None instead of the correct value 40 ```

#4

Alright, there are a few problems with your code; The first problem can be found here:

  if days>=7:
    cost -= 50
    return cost
  elif days>=3:
      cost-=20
      return cost

You have used inconsistent indentation. In Python, where indentation is extremely important, it is highly recommended that you always use 4 spaces of indentation.

The other problem can be found here:

def trip_cost(city ,days):
  return 140*days + city + cost

You are using the variable cost in this calculation. However, cost has neither been defined globally nor within the scope of this function. Also, why are you doing 140 * days when you already have a function that does this for you (i.e. hotel_cost())?

Another issue with this function, is that city cannot be added to days, as days will be an integer and city will be a string. Besides, we don’t want to add city to these other numbers anyway, we want the cost of the plane ride to city, which is why we made a function earlier called plane_ride_cost().

Lastly, you don’t need to add days to the function, but you do need to add the cost for renting a car for days. You made a function for this as well, it’s called rental_car_cost().

Therefore, the function should actually look like this:

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

#5

Merci pour votre réponse


#6

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