Python中for循环搭配else的陷阱
Posted 小学弟-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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子句。
以上是关于Python中for循环搭配else的陷阱的主要内容,如果未能解决你的问题,请参考以下文章