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.
items
(a tuple) toadd
, meaning thatargs
is:[(5, 7, 4, 12)]