-1

I'm getting a TypeError while using a tuple in a multiple arguments function. Here's my code:

def add(*args):
    result = 0
    for x in args:
        result = result + x
    return result

items = 5, 7, 4, 12
total = add(items)
print(total)

This is the error:

Traceback (most recent call last):
  File "e:\functions.py", line 9, in <module>
    total = add(items)
  File "e:\functions.py", line 4, in add
    result = result + x
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

I'm not getting any error if I directly enter the arguments instead of using a variable:

total = add(5, 7, 4, 12)

I've coded in Java and I just started using Python and I can't figure out why this is happening.

2
  • 1
    You are passing the single argument items (a tuple) to add, meaning that args is: [(5, 7, 4, 12)]
    – dwb
    Commented Dec 29, 2020 at 16:50
  • 1
    Does this answer your question? What does asterisk * mean in Python?
    – pippo1980
    Commented Dec 29, 2020 at 17:36

3 Answers 3

4

You're passing the tuple items as a single argument to add, which is written to expect an arbitrary number of individual numeric arguments rather than a single iterable argument (that's what the *args syntax does -- it takes an artitrary number of arguments and converts them to an iterable inside the function).

The TypeError is happening because your for x in args is getting the value of items as its first value of x (since it's the first argument), and so your function is trying to do the operation 0 + (5, 7, 4, 12), which is not valid because you can't add an int to a tuple (which is why the error message says that).

To pass the individual items as individual args, do:

total = add(5, 7, 4, 12)

or unpack the tuple by mirroring the * syntax in the caller, like this:

total = add(*items)

Note that Python has a builtin function called sum that will do exactly what you want to do with your tuple:

total = sum(items)

You could get the same behavior from your add function by removing the * from the *args in your function definition.

1
  • Beat me to it. the unpacking of items should do the trick for the OP. Commented Dec 29, 2020 at 16:56
2

When you do this.

items = 5, 7, 4, 12 #tuple & it looks like this (5,7,4,12)
total = add(items)

You pass your items variable to your add function & in general it would look like this.

total = add((5,7,4,12))#Not like this add(5,7,4,12)

Well...this is correct there is nothing wrong with it but base on your objective this is not the right way. Learn more about *args here.

This is what you are expecting to do & you can do this by unpacking as what the other answer suggests.

add(5,7,4,12)

Cause what you did is you passed the whole tuple so your args parameter would look like this ((5,7,4,12)) & when you do a for loop you are iterating the tuple (which is the args) object's value which is this (5,7,4,12) & then adding it to an int which is clearly an error as stated.

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
1
def add(*args):
    result = 0
    for x in args:
        result = result + x
    return result

items = 5, 7, 4, 12
total = add(*items)
print(total)

just add a random * in total = add(*items)

result:

28

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

What does asterisk * mean in Python? [duplicate]

Not the answer you're looking for? Browse other questions tagged or ask your own question.