Else clause on Python while statement
我注意到以下代码在Python中是合法的。我的问题是为什么?有什么特别的原因吗?
1 2 3 4 5 6 | n = 5 while n != 0: print n n -= 1 else: print"what the..." |
只有当您的
考虑它的一种方法是作为条件的if/else构造:
1 2 3 4 | if condition: handle_true() else: handle_false() |
类似于循环结构:
1 2 3 4 5 | while condition: handle_true() else: # condition is false now, handle and go on with the rest of the program handle_false() |
一个例子可能是:
1 2 3 4 5 6 7 8 | while value < threshold: if not process_acceptable_value(value): # something went wrong, exit the loop; don't pass go, don't collect 200 break value = update(value) else: # value >= threshold; pass go, collect 200 handle_threshold_reached() |
如果您正常退出一个块,则执行
通常情况下,您会在提前退出循环的地方找到它,而从循环的末尾跑掉是一种意外/不寻常的情况。例如,如果您在一个列表中循环查找一个值:
1 2 3 4 5 6 | for value in values: if value == 5: print"Found it!" break else: print"Nowhere to be found. :-(" |
作为对
这就是它的工作原理:外部循环在末尾有一个中断,所以它只能执行一次。但是,如果内部循环完成(没有找到除数),那么它将到达else语句,并且永远不会到达外部中断。这样,内部循环的中断将从两个循环中中断,而不仅仅是一个循环。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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!' |
对于
在大多数情况下,有更好的方法可以做到这一点(将它包装成一个函数或引发一个异常),但这是可行的!
当while条件的计算结果为false时,将执行else子句。
从文档中:
The while statement is used for repeated execution as long as an expression is true:
1
2 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 theelse clause’s suite. Acontinue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
我的答案将集中在我们什么时候可以使用while/for else上。
乍一看,使用时似乎没有什么不同
1 2 3 4 | while CONDITION: EXPRESSIONS print 'ELSE' print 'The next statement' |
和
1 2 3 4 5 | while CONDITION: EXPRESSIONS else: print 'ELSE' print 'The next statement' |
因为在这两种情况下(都是在
那么,只有当语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
如果不同:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
异常提升也不会引起差异,因为当它提升时,将执行的下一个代码在异常处理程序(块除外)中,不会执行
它对社会交往很有用。
1 2 3 4 | while (Date !="January 1st"): time.sleep(1) else: print("Happy new year!") |
当且仅当while循环不再满足其条件时(在您的示例中,当
所以输出结果是:
1 2 3 4 5 6 | 5 4 3 2 1 what the... |
python中"while:else:"构造的更好用法应该是,如果"while"中没有执行循环,则执行"else"语句。今天它的工作方式没有意义,因为您可以使用下面的代码并获得相同的结果…
1 2 3 4 5 | n = 5 while n != 0: print n n -= 1 print"what the..." |