I'm having a hard time in an exercise where I should show the sum of all elements of a list recursively.
The code that I just got has the basis of recursion, the recursion itself did not, because I did not understand how it could be applied to any list.
I'll leave the code here to review:
def soma_lista(lista):
if len(lista) == 1:
return lista[0]
else:
for i in range (len(lista)):
soma = lista[i] + soma_lista(lista[i + 1])
return soma
NOTE: As I said, I do not know how to apply the recursion part; so I tried something random. Ignore the else's code block.