Need explanation about last lesson in Taking a Vacation


#1

Hello :slight_smile:

Here is my code. The problem is not in my code, cause it seems to work fine. I just can’t understand how they can change the nights in hotel_cost(nights) to hotel_cost(days - 1) in total_cost and still come up with the right amount. The cost of 1 night is defined with 140 dollar. But the cost of 2 days is not defined with 140 dollar. I just want to understand why and how they can change the arguments without any difficulty or a wrong outcome. Can anyone explane?

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 = 40 * days
    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)

Greetings!


#2

parameters vs arguments. When we define a function we can give it a parameter:

def example(my_parameter):
   print my_parameter

then when we call the function, we can supply an argument for the parameter:

example("hello world")

in your code, we didn’t change anything, hotel_cost(days - 1) is a function call, so its simply an argument being passed to the parameter (nights)


#3

Thank you for your answer! Didn’t understand it right away, but I get it now.


#4

If put together the pieces of code i provided:

def example(my_parameter):
   print my_parameter

example("hello world")

it should become pretty clear. I on purpose left it out :wink:

Also, i went with a very basic example and was hoping you would figure out that we can also call function from inside another function. By letting you take these thinking steps yourself, you hopefully learned more :slight_smile:


#5

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