5/7 Taking a vacation


I don’t have any error messages in particular, but I do have a question about why exactly this works

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):
return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city)


I had it automatically give me the solution because I am extremely confused as to WHY exactly this works. More specifically,

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

In the instructions it says to call hotel_cost only with the argument (days -1). I don’t understand how we can call a function with an argument not previously specified as a valid argument for hotel_cost? How can we call “days” from a function who’s (to my knowledge) only previously specified argument is “nights” (as per the first line of the code)?


Variables aren’t global if that’s what you mean. (Or why else would their names need to match?)


I’m still confused, honestly. How am I calling hotel_cost with a different argument for it than the one I previously defined for it in the first lines of the code? Why don’t I need to modify the arguments it accepts? From my point of view it looks like hotel_cost should only accept nights as an argument, yet in the last line I’m now telling it to accept days instead of nights

def hotel_cost(nights):

In the first line, from what I think I understand, I’m defining hotel_cost and telling it to accept nights as an argument.

but then, in the last line, we’re suddenly telling it to act upon days, not nights.

return rental_car_cost(days) + hotel_cost(days - 1)

This seems to go against what I understand about variables and arguments


Think about it like this
Your function hotel_cost() it takes an integer as an argument
if you call hotel_cost(1) it tells you the price for one night at the hotel
If i’m staying at a place for 5 days I’m gonna need the hotel for 4 nights aka days minus one
So I use hotel_cost(4) to get my rate

In your program a user is going to give you the number of days the trip lasts.
You save it as a variable called days
User: My trip will be for 3 days
Program: days = 3

return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city)
It calls those functions by plugging the integer days into the function calls.
You see the name of the variable never mattered at all, the TYPE of data is what matters. The function expects an integer. days is in fact an integer, just as nights represents an integer.


It doesn’t act on days or nights or pigs, horses, water coolers. Name it whatever you want. The name isn’t what’s passed to the function.


That makes more sense to me. I thought it had to specifically be whatever I named it. That’s why I was so confused.

Thanks for the quick replies :slight_smile:


When you call something, you may pass along references to values. I say references because the values themselves are not copied.

In simplified terms there is a global list where all the current calls and such are placed. This is called the stack.
When you call something, what happens is that arguments get appended to that list, and probably also something saying what the current location in the code is, and then control jumps to the function that is being called. That function removes those values, does something, puts the result in that list, and jumps back to the last location (returns)

(simplified as in I’m not claiming this is exactly what happens, but it’s the general idea)


This is much more difficult to learn than I ever expected. I feel like I’m taking 2 steps backwards every step forward.


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