@Cigano Morrison has already given you the solution to this problem. But, if you will allow me to give you correction suggestions:
Why does i = 0
start? It is not necessary, the value of i
in the first loop of the loop is already going to be 0 because its xrange(0,len(lista)): ...
starts at 0.
Failure to increment 1 to i
at the end of the loop, construction of for
cycle is already doing this automatically
That is, it would look like:
lista = [1,2,3,4]
soma = lista[0]
for i in xrange(0, len(lista) - 1):
if lista[i] != lista[i + 1]:
soma += lista[i + 1]
print soma # 10
Note that with this code you will only add soma
if two list values are not equal or consecutive, because of the if lista[i] != lista[i + 1]: ...
condition.
In other words, if the list was [1,2,2,2]
, the result would be 3, since the second and last 2 ( lista[2]
, lista[3]
), which have values equal to the previous elements, would not be counted. >
PS: Beware of the title of the question "Summing consecutive integers", because what you are doing with this code (if it is the same logic that presented) is more "Summing integers that are not equal or consecutive"