def listsum(numList): if len(numList) == 1: return numList else: return numList + listsum(numList[1:]) print(listsum([1,3,5,7,9]))
Here is what the above code is Doing:
1. The function listsum takes a list of numbers as its argument.
2. The function calls itself, each time with a smaller version of the list.
3. The list keeps getting smaller until it only has one element.
4. At that point, the function returns the value of that element.
5. The returned values are added together.