How can I add two arguments to the same function?


#1

In the instructions, the user is asked to "define a function called trip_cost that takes two arguments, city and days.".
However, when I try to do this, def trip_cost (city, days):
the console returns " File "python", line 19
def trip_cost (city days):
^
SyntaxError: invalid syntax "
Any ideas?


#2

Be sure they are separated by a comma.

(city, days)

#3

I tried this, and it returned:
File "python", line 19
def trip_cost(city, days):
^
IndentationError: expected an indented block

the ^ should be pointing to the : but for some reason it shows in the wrong place


#4

Please post your code so we can have a closer look. Thanks.


#5

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):


#6

There is no code block corresponding with the header. The interpreter is expecting one.

def trip_cost(city, days):
    return # hotel cost, rental car cost, plane ride cost

Your return value with the sum of the returns from your other three functions which are all called from within this function.


#7

I think I have that sorted out now, but how do I find the sum of the days and the city? I can't remember if there's a way to combine an int and str with the sum() function.


#8

In your tirp_cost function, you will be making three calls out to the other functions.

hotel_cost(days)
car_rental_cost(days)
plane_ride_cost(city)

It's your option as to whether to assign these to temporary variables, or return the total.

a = hotel_cost(days)
b = car_rental_cost(days)
c = plane_ride_cost(city)
return a + b + c

or

return hotel_cost(days) + car_rental_cost(days) + plane_ride_cost(city)

Either way, it is a single value that is returned.

On a fancier note, we have seen this as a submission, but rarely...

return sum([hotel_cost(days), car_rental_cost(days), plane_ride_cost(city)])

#9

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