The problem is that array.index()
will return the first instance of a value. You have the value 84
twice - so since the first index is odd, you never add it.
You really need to keep track of the index, not rely on uniqueness of the values. You do this with
for idx, val in enumerate(array):
now your first value will be the index, and the second value will be the value. Test idx%2==0
and you can figure it out from here.
update here is the complete code, making clear (I hope) how this works:
checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]
def checkzi(array):
if len(array) != 0:
sum_array = 0
for idx, x in enumerate(array):
print "testing element", idx, " which has value ", x
if (idx % 2 == 0):
sum_array += x
print "sum is now ", sum_array
else:
print "odd element - not summing"
print (sum_array)
answer = (sum_array) * (array[len(array)-1])
return (answer)
else:
return 0
checkzi(checkio)
Output:
testing element 0 which has value -37
sum is now -37
testing element 1 which has value -36
odd element - not summing
testing element 2 which has value -19
sum is now -56
testing element 3 which has value -99
odd element - not summing
testing element 4 which has value 29
sum is now -27
testing element 5 which has value 20
odd element - not summing
testing element 6 which has value 3
sum is now -24
testing element 7 which has value -7
odd element - not summing
testing element 8 which has value -64
sum is now -88
testing element 9 which has value 84
odd element - not summing
testing element 10 which has value 36
sum is now -52
testing element 11 which has value 62
odd element - not summing
testing element 12 which has value 26
sum is now -26
testing element 13 which has value -76
odd element - not summing
testing element 14 which has value 55
sum is now 29
testing element 15 which has value -24
odd element - not summing
testing element 16 which has value 84
sum is now 113
testing element 17 which has value 49
odd element - not summing
testing element 18 which has value -65
sum is now 48
testing element 19 which has value 41
odd element - not summing
48
You obviously want to take the print
statements out - I added them to help explain the program flow.