def add_function(x,y):
return sum(x + y)

why doesn’t that work but this does:

def add_function(x,y):
return x + y


sum() is a built-in function for an iterable like a list, its not designed for adding integers together


Keep in mind that x + y will be evaluated before sum is even called, so if x=1 and y=2, then you’d be doing sum(3)
Like mentioned it also won’t accept multiple arguments: sum(x, y)
What you’d need is an iterable: sum([x, y]) – but again, creating an iterable to use sum isn’t that helpful when the + operator exists


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