recursion sum evens can somebody answer | Sololearn: Learn to code for FREE!

0

recursion sum evens can somebody answer

## This is where i need help this is the function and what I need it to do # Recursive Summation (evens only) # > Sums all even values in a list # > Search begins from the end of the list (searches backwards) def sumEvens(list, i): if i == len(list): return 0 elif list[i]%2==0: return list[i] + sumEvens(list,i+1) else: return sumEvens(list,i+1) ############## SUMEVEN TESTS ############### list = generateList(10) print('Sum:', sumEvens(list, len(list)-1)) print() list = generateList(15) print('Sum:', sumEvens(list, len(list)-1)) print() ^^ here's my test case, I've been playing around but it keeps returning weird numbers can anybody help it has to consist of this same if elif return elif return else return structure def sumEvens(list, I): if ___?__: return ___?__ elif ___?__: return ___?__ else: return ___?__ '''

5/2/2021 2:26:50 AM

Gutz_X_73vEn

3 Answers

New Answer

+1

I think I figured it out def sumEvens(list, i): if i < 0: return 0 elif list[i]%2==0: return list[i] + sumEvens(list,i-1) else: return sumEvens(list,i-1)

+1

def sumEvens(list, i): if i == -1: return 0 elif list[i]%2==0: return list[i] + sumEvens(list,i-1) else: return sumEvens(list,i-1) print(sumEvens([1,2,3,4,5], 4))

0

I cant change the test case or the structure of the function