5/8 Sequance of arguments


#1

Hi everyone!
Could anybody explain me why I can place arguments only in that order "def trip_cost(city, days):" to make my code work?
If I use "def trip_cost(days, city):" an error appears.

The code
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
else: "YOU MUST CHOOSE!"
def rental_car_cost(days):
car = 40 * days
if days >= 7:
car -= 50
elif days >= 3:
car -= 20
return car
def trip_cost(city, days):
return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)


#2

@disconnect4639

Well, you can put the arguments in any order as long as you follow the pattern of, args then kwargs.

If you call the function though and do not call it by name in the args/kwargs part and you changed the order in your call it will bug out.

def stuff(string, integer):
    print(int(integer), str(string))

string1 = 'string'
integer1 = 5

works

stuff(string1, integer1)

Does not work

stuff(integer1, string1)

#3

@zeziba
Same problem with this code. Until I haven't changed the sequence of arguments (initial "def trip_cost(city, spending_money, days):", it didn't work. So I don't fully understand. As you said before, first of all args then kwargs, "spending_money" - is a kwarg or an arg? Does a sequence only depends on a previous code or it also depends on a consequent too?

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
else: "YOU MUST CHOOSE!"
def rental_car_cost(days):
car = 40 * days
if days >= 7:
car -= 50
elif days >= 3:
car -= 20
return car
def trip_cost(city, days, spending_money):
return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city) + spending_money

Thanks in advance!


I dont understand 3/7
#4

I do have to say I loath how they present the material in this part of the course. I have posted some code that does work like it should I do not know if it will pass the course though. Might have to change some things, but I do hate that they made us use so many if/else in the one function when we have the tools by this course to do it as I have shown. Which is easy to read and maintain then the garbage they showed us.

They should have had us do something along these lines.

EXAMPLE:

city_prices = {
    "Charlotte": 183,
    "Pittsburgh": 222,
    "Tampa": 220,
    "Los Angeles": 475
}

def hotel_cost(nights):
    return 140 * nights

def plane_ride_cost(city):
    return city_prices[city]

def rental_car_cost(days):
    car = 40 * days
    if days >= 7:
        car -= 50
    elif days >= 3:
        car -= 20
    return car

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

print(trip_cost(city='Charlotte', days=5, spending_money=1250))

@disconnect4639

When you do the call like print(trip_cost(city='Charlotte', days=5, spending_money=1250)) if you name the argument that is called a kew word argument, if you just enter it in then it's an argument.
example: print(trip_cost('Charlotte', 5, 1250))


Part 5 "Pulling It All Together"