Pulling it together, global name 'nights' is not defined


#1


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


Oops, try again.
trip_cost('Charlotte', 4) raised an error: global name 'nights' is not defined

Oops, try again.
trip_cost('Los Angeles', 6) raised an error: global name 'nights' is not defined

The city and days variables change everytime I run the code.


Get the total cost of everything back


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


#2

This function:

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

Where does the nights come from? You pass two arguments into the function, city and days, no nights. Change nights to days.

If you where to add a function call:

print trip_cost('Los Angeles',4)

you would see this error being raised


#3

But how come when I type this in, I get None in the console and yet codeacademy.com is telling me it's correct?


#4

Well, there is nothing printed, so the None gets printed. But you could add a print statement at the end:

print trip_cost('Tampa',5)

#5

@stetim94
So then, nothing will ever get printed to console unless you use the print function?


#6

Well, there are a few more function who print to a console/window, but yes.

People have no idea how complex code is, they only see a window with some printed text and a few buttons