I am trying to do multiplication recursion (multiplying all the values of a container) in Python. The function receives the elements of the list as positional argument(*n). On execution I receive the error saying "Maximum recursion depth reached". However, the code works fine if I simply use n instead of *n and send the elements in a list.
Code not working:
def multiply(*n):
if n:
return n[0]*multiply(n[1:])
else:
return 1
multiply(5,1,4,9)
Working code:
def multiply(n):
if n:
return n[0]*multiply(n[1:])
else:
return 1
multiply([5,1,4,9])