I don't think I understand definitions


#1

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
    else:
        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
    else: 
        return days * 40
def trip_cost(city, days):
    return hotel_cost + plane_ride_cost + rental_car_cost

#2

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

funcName()

So your trip_cost() function will look like this:

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

#3

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.


#4

Thanks. I think I understand it now.


#5