Define trip_cost - 5/7 Pull It Together


#1

Not sure why I keep getting this error to define trip_cost:

def hotel_cost(nights):
  return 140 * nights

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):
  rate = 40
  if days >= 7:
    total = (rate * days) - 50
    return total
  elif days >= 3:
    total = (rate * days) - 20
    return total
  else:
    total = rate * days
    return total
  
  def trip_cost(city, days):
    return plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days - 1)

#3

Unless directed to, leave off the - 1.


#4

It does require that.


#5

Pull it together

I just did the lesson from scratch and passed. Error messages don’t always point to the error, and SCT messages are limited. Any error in the code could result in the same message.

Look for ways to simplify your code and don’t add things that are not suggested or asked for in the instructions. This can lead to unexpected code patterns that the SCT will reject out of hand.

rate = 40

first victim of cutbacks. Try instead a single expression that computes undiscounted cost…

cost = days * 40

Likewise, the total variable is not asked for and in truth is excess verbiage. If you’re not going to do any further computation, then declaring a variable before returning it is wasteful and totally unnecessary. That sort of code is easy to trim.

Knowing what cost is it should be a simple matter to apply the discount directly in the conditional, if one applies. Depending which school of thought you are expected to apply, either return the discounted amount immediately, or discount the amount and store that, then return it at the end of the function.

if ...:
    return cost - 50

or,

if ...:
    cost -= 50
...
return cost

The choice is yours.


#6

I rewrote it cutting out the excess variables and it still returned “Make sure to define a function named trip_cost”.

I ended up clicking the Solution button, but even then when I run that code (see below) it returns the same error. Not sure whats up.

def hotel_cost(nights):
  return 140 * nights

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:
    cost -= 50
  elif days >= 3:
    cost -= 20
  return cost

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

#7

Closing the tab and re-opening this lesson and running the Solution code is working now.


#8

It isn’t asked for, but what if you try to call your function just to test it?


#9

Looks like the indenting at your def trip_cost is off. So it might think your trip_cost is part of the rental_car_cost…

Could be wrong though :sweat_smile:


#10

Huh? Looks fine to us.


#11


#12

But it looks like the OP addressed this in their posting of the code.


#13

Ah I missed that … :face_with_raised_eyebrow:


#14

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