I learned a function that receives multiple inputs, and then I realized that it was a def function add_many (1,2,3,4,5,6,7,8,9) is added and executed I'm curious about the reason why it's error if I use add_many(range(1,10))). I thought add_many(1,~~,9) and add_many(range(1,10)) were the same.
def add_many(*args):
result = 0
for i in args:
result = result + i
return result
result = add_many(range(1,10))
print(result)
TypeError: unsupported operand type(s) for +: 'int' and 'range'
function python
>>> def f(*arg):
return sum(arg)
>>> f(range(3))
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
f(range(3))
File "<pyshell#2>", line 2, in f
return sum(arg)
TypeError: unsupported operand type(s) for +: 'int' and 'range'
>>> f(*range(3))
3
>>> f(*range(10))
45
>>> def f(*arg):
print(arg)
return sum(arg)
>>> f(range(3))
(range(0, 3),)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f(range(3))
File "<pyshell#7>", line 3, in f
return sum(arg)
TypeError: unsupported operand type(s) for +: 'int' and 'range'
>>> f(*range(3))
(0, 1, 2)
3
© 2024 OneMinuteCode. All rights reserved.