144k views
5 votes
write a recursion (a function) sum list (nums) that calculates the sum of numbers in a list, the parameter nums is a list of integers. if the list is empty, it should return none.

User MarkH
by
4.6k points

1 Answer

7 votes

def eval(n):

return n[0] + eval(n[1:]) if n else 0

list = []

idx = int(input("How many elements will you append?: "))

for i in range(idx):

tmp = int(input("Enter a number: "))

list.append(tmp)

print("The sum of the list is:",eval(list))

write a recursion (a function) sum list (nums) that calculates the sum of numbers-example-1
User Pyttroll
by
5.0k points