python,如何跳过范围循环中的行

时间:2019-01-19 21:09:20

标签: python loops range

在python语言中,我想跳过范围循环(或xrange)的行而不破坏循环,如下所示:

for i in range(10):
    ... some code happening
    ... some code happening

    if (some statement == True):
        skip the next lines of the loop, but do not break the loop until finished

    ... some code happening
    ... some code happening

2 个答案:

答案 0 :(得分:2)

使用continue

for i in range(10):
    if i == 5:
        continue
    print(i)

输出:

 0
 1
 2
 3
 4
 6
 7
 8
 9

答案 1 :(得分:1)

您可以只在条件中嵌套该块:

for i in range(10):
    ... some code happening
    ... some code happening

    if not some_statement:

        ... some code happening
        ... some code happening