I don't think I understand definitions


Okay, I don't think I understand definitions, really. How does this work? With this def travel_cost function, can't I just call hotel_cost, plane_cost and rental_cost? I don't understand. Please help!

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
        return "I'm sorry, that city is not in our database. Please choose from the following: Charlotte, Tampa, Pittsbugh or Los Angeles. Thank you"
def rental_car_cost(days):
    if days >= 7: 
        return days * 40 - 50
    elif not days >= 7 and days >= 3:
        return days * 40 - 20
        return days * 40
def trip_cost(city, days):
    return hotel_cost + plane_ride_cost + rental_car_cost


When you call a function, just like defining one, it needs to end with the brackets, like so:


So your trip_cost() function will look like this:

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


Hi @thepykid,

Inside the trip_cost function, you have this ...

    return hotel_cost + plane_ride_cost + rental_car_cost

As @toplearner indicated, including a function name in an expression without following that name with parentheses and arguments, if necessary, does not call the function.

The hotel_cost, plane_ride_cost, and rental_car_cost functions each have a parameter in the header of their function definitions. Therefore, each of them requires an argument when called. Accordingly, your statement should be ...

    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

The above statement passes on the city and days information that is supplied to the trip_cost function when it is called, for processing by the hotel_cost, plane_ride_cost, and rental_car_cost functions.


Thanks. I think I understand it now.