0
Recursive variable without using global?
Hello, so I have made a recursive function that returns a sum of all negative numbers in a list as what I wrote here: https://code.sololearn.com/cfhhP5DEV3AU/?ref=app is there a way to not use global in this function?
2 Réponses
+ 2
Pass "result" as an argument to your function.
def addall(x, result=0):
if len(x) == 0:
print(result)
else:
if x[0] < 0:
result += x[0]
addall(x[1:], result)
else:
addall(x[1:], result)
+ 1
Diego Thank you