Python中for循环搭配else的陷阱

>When the items are exhausted (which is immediately when the sequence is empty), the suite in 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 continues with the next item,

or with the else clause if there was no next item.

 

根据官方文档说法:大意是说当迭代的对象迭代完并为空时,位于else的子句将执行,

而如果在for循环中含有break时则直接终止循环,并不会执行else子句。

posted @ 2018-03-11 11:01  小学弟-  阅读(312)  评论(0编辑  收藏  举报