I am trying to count the occurrence of '-1' in the list 'args'. '-1' occurs at many locations, so when ever it occurs more than once consecutively, I wish to count that.
I am getting an "list index out of range" error, however it is wrong. I am trying to access the 16th element, the length of 'args' is 19. On line 5 and 6 I am individually printing the index and the element of the list, these lines are executing without error.
Why am I getting the error? Moreover, print statement in line 10 is not printing, what is the reason?
args=[-3, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1, -1, -1, -3, -1, -2, -1, -1, -1]
i=0
while i<= len(args)-1:
count=0
print(i)
print(args[i])
while args[i]==-1:
count+=1
i+=1
print("count="+str(count)+"-"+str(i))
i+=1
$python main.py
0
-3
count=0-0
1
-1
count=4-5
6
-1
count=2-8
9
-1
count=4-13
14
-1
count=1-15
16
-1
Traceback (most recent call last):
File "main.py", line 8, in <module>
while args[i]==-1:
IndexError: list index out of range