Using sum(x,y,z) vs. x + y + z


#1


I figured out how to get my code up and running. Sweet.
Only through some trial and error. Here's my question: When I use sum() to return the values of the three functions, it returns and error message saying that the code expected to return two arguments, and instead got three (which I'm assuming is the three functions).

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

Error message reads as:

trip_cost('Tampa', 9) raised an error: sum expected at most 2 arguments, got 3

Then I changed the code to

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

And it passed. Can anyone shed some light on this?



#2

It happened the same to me. The sum function only receives an argument that is iterable, meaning a list (of int). Check out the documentation!

Try some variations to fully understand it :slight_smile:

list = [3,2,5,1] 
a = sum(list)
print (a)
# console shows 11

a = sum([3,2,5,1])
print (a)
# console shows 11

list = [3,2,5,1] 
a = sum(list,5)
print (a)
# console shows 16

a = sum([3,2,5,1],5)
print (a)
# console shows 16

#3

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