Global name 'nights' is not defined?


#1

This is the code:

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 plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(nights)

now I didn’t have a problem with the nights variable when I wrote the code before, it appeared when I was doing the last step (the sum)
that’s the error: trip_cost(‘Pittsburgh’, 4) raised an error: global name ‘nights’ is not defined
Run
Note: I tried to hit run again without changing anything and the city changes!


#2

here:

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

nights is undefined, it should be days (the function parameter value)


#3

Codecademy subjects your code to a submission correctness test (SCT) for each exercise. In some cases, the SCT might vary the data that it passes to your functions during the testing.


#4

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