recursion sum evens can somebody answer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ___?__ '''

2nd May 2021, 2:26 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
3 Answers
+ 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)
2nd May 2021, 2:43 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 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))
2nd May 2021, 3:05 AM
Infinity
Infinity - avatar
0
I cant change the test case or the structure of the function
2nd May 2021, 2:27 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar