yet one more exercise that I seem to have a problem with. I'd say I've got it right, but Python knows better.
The body of the task is:
Write a function that takes a list or tuple of numbers. Return a two-element list, containing (respectively) the sum of the even-indexed numbers and the sum of the odd-indexed numbers. So calling the function as even_odd_sums([10, 20, 30, 40, 50, 60]) , you’ll get back [90, 120] .
My code is:
def even_odd_sums(sequence):sum_list = []for i, v in enumerate(sequence):if i % 2 == 0:sum_list = sum_list.insert(0, sum(v))else:sum_list = sum_list.insert(1, sum(v))return sum_listprint(even_odd_sums([10,20,30,40,50,60]))
the result is:
TypeError Traceback (most recent call last)
<ipython-input-60-14518295929c> in <module>
----> 1 print(even_odd_sums([10,20,30,40,50,60]))<ipython-input-59-51fcb6e9a115> in even_odd_sums(sequence)3 for i, v in enumerate(sequence):4 if i % 2 == 0:
----> 5 sum_list = sum_list.insert(0, sum(v))6 else:7 sum_list = sum_list.insert(1, sum(v))TypeError: 'int' object is not iterable
I tried to find the solution on Google, I tried other ways to solve this task ("for i in range(len(sequence)), but I just can't solve non-iterable object problem
Thank you in advance!