我注意到下面的代码在Python中是合法的。我的问题是为什么?有什么特别的原因吗?

n = 5
while n != 0:
    print n
    n -= 1
else:
    print "what the..."

许多初学者在尝试在while或for循环中放入if/else块时无意中发现了这种语法,并且没有正确地缩进else。解决方案是确保else块与if块对齐,假设您打算对它们进行配对。这个问题解释了为什么它没有导致语法错误,以及产生的代码意味着什么。我还得到了一个IndentationError。我该怎么解决呢?,用于报告语法错误的情况。


当前回答

在Python中,'while: else:'结构的更好用法应该是,如果'while'中没有执行循环,则执行'else'语句。今天它的工作方式没有意义,因为您可以使用下面的代码获得相同的结果……

n = 5
while n != 0:
    print n
    n -= 1
print "what the..."

其他回答

当while-condition的值为false时,else子句将被执行。

从文档中可以看到:

The while statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates. A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

我的回答将集中在我们什么时候可以使用while/for-else。

乍一看,使用时似乎没有什么不同

while CONDITION:
    EXPRESSIONS
print 'ELSE'
print 'The next statement'

and

while CONDITION:
    EXPRESSIONS
else:
    print 'ELSE'
print 'The next statement'

因为打印'ELSE'语句似乎总是在这两种情况下执行(当while循环结束或不运行时)。

然后,只有当打印'ELSE'语句不被执行时,情况才有所不同。 它是在while下面的代码块中有一个突破

In [17]: i = 0

In [18]: while i < 5:
    print i
    if i == 2:
        break
    i = i +1
else:
    print 'ELSE'
print 'The next statement'
   ....:
0
1
2
The next statement

如与:

In [19]: i = 0

In [20]: while i < 5:
    print i
    if i == 2:
        break
    i = i +1
print 'ELSE'
print 'The next statement'
   ....:
0
1
2
ELSE
The next statement

Return不在此类别中,因为它对上述两种情况具有相同的效果。

异常引发也不会造成差异,因为当它引发时,下一个代码将在异常处理程序中执行(block除外),else子句或while子句后面的代码将不会被执行。

As far as I know the main reason for adding else to loops in any language is in cases when the iterator is not on in your control. Imagine the iterator is on a server and you just give it a signal to fetch the next 100 records of data. You want the loop to go on as long as the length of the data received is 100. If it is less, you need it to go one more times and then end it. There are many other situations where you have no control over the last iteration. Having the option to add an else in these cases makes everything much easier.

请允许我举例说明为什么要使用else子句。但是:

我的观点在Leo的回答中得到了更好的解释 我使用for-而不是while-循环,但else工作类似(除非遇到break,否则执行) 有更好的方法可以做到这一点(例如,将其包装到函数中或引发异常)

打破多级循环

它是这样工作的:外部循环在结尾有一个break,所以它只会执行一次。但是,如果内部循环完成(没有找到除数),那么它将到达else语句,并且永远不会到达外部断点。这样,内部循环中的中断将跳出两个循环,而不仅仅是一个循环。

for k in [2, 3, 5, 7, 11, 13, 17, 25]:
    for m in range(2, 10):
        if k == m:
            continue
        print 'trying %s %% %s' % (k, m)
        if k % m == 0:
            print 'found a divisor: %d %% %d; breaking out of loop' % (k, m)
            break
    else:
        continue
    print 'breaking another level of loop'
    break
else:
    print 'no divisor could be found!'

我知道这是个老问题,但是…

正如Raymond Hettinger所说,应该调用while/no_break而不是while/else。 如果你看一下这个片段,我发现它很容易理解。

n = 5
while n > 0:
    print n
    n -= 1
    if n == 2:
        break
if n == 0:
    print n

现在,我们不用在while循环后检查condition,而是用else替换它,然后去掉那个检查。

n = 5
while n > 0:
    print n
    n -= 1
    if n == 2:
        break
else:  # read it as "no_break"
    print n

我总是把它读成while/no_break来理解代码,这种语法对我来说更有意义。