Arguments order question


#1


https://www.codecademy.com/en/courses/python-beginner-P5YtY/1/4?curriculum_id=4f89dab3d788890003000096#


trip_cost('Pittsburgh', 3) raised an error: unsupported operand type(s) for -: 'str' and 'int'


Does the order that I place the arguments in matter? If I swap 'city' and 'days' it seems to work fine, but I'm not sure why.


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


#2

This is known as positional arguments/parameters, the value of first argument gets copied into first parameter and the value of second argument gets copied into the second parameter

so yes, order of parameters and arguments matter


#3

In my posted code, though, it only seems to work when 'city' is placed before 'days'
By that logic, shouldn't my original posted code work by copying 'days' into the parameters first?

Also, what happens when there are 3 parameters like in this case?

Thanks


#4

but the exercise calls with the city as first argument and days as second argument:

def trip_cost(days, city):
    return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)
print trip_cost('Pittsburgh', 3)

and that won't work. given days now has a value of Pittsburgh and city a value of 3, so now hotel_cost is called with argument days which has value Pittsburgh, something hotel_cost can't handle

This is why the exercise throws an error

trip_cost has 2 parameters, city and days. You simply use the parameters multiply times inside the function. Nothing special happens, its like using a variable multiply times.


#5

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