Plan Your Trip! (Questions on arguments and sequencing)


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):
    rental_car_cost = 40 * days
    if days >= 7:
        rental_car_cost -= 50
    elif days >= 3:
        rental_car_cost -= 20
    return rental_car_cost
def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money
print trip_cost("Los Angeles", 5, 600)

Why is it that the function hotel_cost() is originally written with (nights), but later on called with argument (days) without any issues being caused with the program? Was there any reason at all to have (nights) as the original argument?
Also, why is it that in the second to last line we ask to return the sum of "rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money" in which the argument (days) comes first, yet on the next line when giving it the arguments for trip_cost the first in the sequence is Los Angeles?
If someone could explain that'd be awesome!


nights is a local variable. We could call it anything. The trip_cost function takes a locally defined variable, days which it passes to hotel_cost. It is important that it uses the variable it has defined. The callee is free to use any name locally.


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