Plan your trip!


#1

I completed the lesson but my question is regarding the last function to call everything. We have four functions but we’re calling three, but in “print” they’re not in order based on function order so how does it know which numbers to use for which function?

I’m probably overthinking this but just want to get a better understanding of how it’s calling these functions and why the order doesn’t matter. Thank you for the help!

def trip_cost(city, days, spending_money):
return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city) + spending_money
print trip_cost(“Los Angeles”, 5, 600)

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, spending_money):
  return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city) + spending_money
print trip_cost("Los Angeles", 5, 600)

#2

yes, we do:

hotel_cost
plane_ride_cost
rental_car_cost
trip_cost

and which 3 of the 4 are you referring to here?

Doesn’t matter? here:

print trip_cost("Los Angeles", 5, 600)

we pass 3 values as argument to function call. This arguments are then passed to the parameters of trip_cost function, here is a visual representation if that helps:

city = "Los Angeles"
days = 5
spending_money = 600

the order of parameters and arguments is important.

then inside the trip_cost function we can pass the parameters (city and days) as arguments to the other 3 (rental_car_cost, hotel_cost and plane_ride_cost) other functions.

As long as we pass city, days to the right functions, everything is fine. In which order we call the functions doesn’t matter, 3 + 2 + 1 is 6, but 1 + 2 + 3 is also 6.


#3

Ahhh ok! I was overlooking and overthinking this. Thank you for taking the time to help me and to break it down, @stetim94

I see now that the arguments I entered in the trip_cost function do actually match up with the parameters entered above.

def trip_cost(city, days, spending_money):
  return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city) + spending_money
print trip_cost("Los Angeles", 5, 600)

#4

yes, they match up. This is known as positional arguments. (given the position/order matters), we like logical naming within programming

So all good now? :slight_smile:


#5

Definitely! You rock


#6

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