# 不中断程序就无法打印索引号I cannot print out my index numbers without breaking program

``````scores = [100,22,33,44,22,56]
``````

``````scores = [100,22,11,22,44,66,77]
position = 0
counter = []
findthis = 22

for x in scores:
position = scores.index(findthis, position)
counter.append(position)
position = position + 1

print(counter)
``````

## 4 个回复4

### ===============>>#1 票数：1

``````scores = [100,22,11,22,44,66,77]
position = 0
counter = []
findthis = 22

while True:
try:
position = scores.index(findthis, position)
counter.append(position)
position = position + 1
except ValueError:
break

print(counter)
``````

### ===============>>#2 票数：1

``````scores = [100,22,11,22,44,66,77]
position = 0
counter = []
findthis = 22

for x in scores:
if x==findthis:
counter.append(position)
position = position + 1
``````

### ===============>>#3 票数：1

``````print([i for i, ele in enumerate(scores) if ele == findthis])
``````

`i`是分数中每个元素的索引， `ele`是每个实际元素。

``````scores[position:]
1st iteration -> [22, 11, 22, 44, 66, 77]
2nd           ->  [22, 44, 66, 77]
3rd           ->  [44, 66, 77] # error
``````

### ===============>>#4 票数：0

``````scores = [100,22,11,22,44,66,77]
counter = []
findthis = 22

for index,value in enumerate(scores):
if value is findthis:
counter.append(index)
``````

``````scores = [100,22,11,22,44,66,77]
findthis = 22

counter=[index for index,value in enumerate(scores) if value is findthis]
``````

3回复

4回复

3回复

3回复

3回复

1回复

7回复

2回复

1回复

5回复