为什么这不好用?

时间:2015-10-30 19:52:26

标签: python-3.x

为什么打印最后一行?该行必须在未找到时打印' word'在所有???

with open(name) as f:
    all_Line-s = f.read_lines()
    for line_Number in range( l en (all_Lines)):
        if word in all_Lines[line_Number]:
            print(word,"found in line {0}".format(line_Number+1))
            continue
    else:
       print(word,'not found')

结果:

'word' found in line 3
'word' found in line 6
'word' found in line 43
'word' not found

我切换'如果'和'其他'并使用休息但是...

1 个答案:

答案 0 :(得分:1)

如果 10-29 22:06:06.885 4819-4819/com.example.bahadir.myapplicationn E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6 10-29 22:06:06.885 4819-4819/com.example.bahadir.myapplicationn E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 10-29 22:06:06.885 4819-4819/com.example.bahadir.myapplicationn E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:308) 10-29 22:06:06.885 4819-4819/com.example.bahadir.myapplicationn E/AndroidRuntime: at com.example.bahadir.myapplicationn.KanalAdapter.getItem(KanalAdapter.java:72) 10-29 22:06:06.885 4819-4819/com.example.bahadir.myapplicationn E/AndroidRuntime: at com.example.bahadir.myapplicationn.KanalAdapter.getItemViewType(KanalAdapter.java:82) 循环完成而未遇到elsefor/else中的for会执行。

您可以使用标志来控制是否打印“未找到”消息:

break