假设有如下代码:

for i in range(10):
if i == 5:
print 'found it! i = %s' % i
else:
print 'not found it ...'

你期望的结果是,当找到5时打印出:

found it! i = 5

实际上打印出来的结果为:

found it! i = 5
not found it ...

显然这不是我们期望的结果。

根据官方文档说法:

>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.

https://docs.python.org/2/reference/compound_stmts.html#the-for-statement

大意是说当迭代的对象迭代完并为空时,位于else的子句将执行,而如果在for循环中含有break时则直接终止循环,并不会执行else子句。

所以正确的写法应该为:

for i in range(10):
if i == 5:
print 'found it! i = %s' % i
break
else:
print 'not found it ...'

当使用pylint检测代码时会提示 Else clause on loop without a break statement (useless-else-on-loop)

所以养成使用pylint检测代码的习惯还是很有必要的,像这种逻辑错误不注意点还是很难发现的。

唔~

Python中for循环搭配else的陷阱的更多相关文章

  1. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  2. python中的循环结构等相关知识

    ==分支结构== 1.单分支:一般用于只会发生一种情况的场景,if #90以上优秀 score=95 if score>90: print("优秀") 2.双分支:一般用于会 ...

  3. python 中 for 循环 if循环 break

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...

  4. 一文了解Python中的循环(for while break continue 嵌套循环...)

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  5. python中for循环的底层实现机制 迭代

    在python中,存在2种循环方式:for循环和while循环. while循环的实现很简单, 其本质就是一个条件语句,自定义条件,当条件满足的时候,不断执行while代码块. 但是for循环,究竟是 ...

  6. python中的循环以及,continue和break的使用

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  7. python中while循环和for循环的定义和详细的使用方法

    1. 循环的定义,反复做某事,具有明确的开始和结束.   2. 在Python中循环有while和for两种方式: While循环:1) 语法结构 >>> while 条件: ... ...

  8. python中的循环和编码,运算符, 格式化输出

    1.while循环 现在让我们来看看python中的while循环  格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...

  9. Python中的循环语句

    Python中有while循环和for循环 下面以一个小例子来说明一下用法,用户输入一些数字,输出这些数字中的最大值和最小值 array = [5,4,3,1] for i in array: pri ...

随机推荐

  1. webpack项目轻松混用css module

    前言 本文讲述css-loader开启css模块功能之后,如何与引用的npm包中样式文件不产生冲突. 比如antd-mobilenpm包的引入.在不做特殊处理的前提下,样式文件将会被转译成css mo ...

  2. 标注的SQL拼接语句

    方案一 exec sp_executesql N' SELECT T0.[WtmCode], T3.[opCode], T3.[opValue], T3.[CondId] FROM [dbo].[OW ...

  3. 原生js :removeClass和addClass

    function removeClass(obj, aClass) { var re = new RegExp('\\b' + aClass + '\\b'); if (obj.className ! ...

  4. MSSQL sqlserver系统函数教程分享

    摘要: 下文收集了sqlserver函数教程,为每一个函数都进行了相关举例说明, 如下所示: sqlserver聚合函数教程: mssql sqlserver avg聚合函数使用简介 mssql sq ...

  5. linux 命令之netstat

    转自:http://www.maomao365.com/?p=699 linux 命令之netstat 在linux中netstat命令的作用是查看TCP/IP网络当前所开放端口,所对应的本地和外地端 ...

  6. 消除Warning: Using a password on the command line interface can be insecure的提示

    最近在部署Zabbix时需要用脚本取得一些MySQL的返回参数,需要是numberic格式的,但是调用脚本时总是输出这一句: Warning: Using a password on the comm ...

  7. VMware 15 pro虚拟机

    VMware虚拟机都到VMware Workstation 15 Pro,真快,VMware 14还没用好

  8. php学习----运算符

    PHP 1.运算符 加减乘除与数学运算无异 但PHP的赋值运算符有两种,分别是: (1)"=":把右边表达式的值赋给左边的运算数.它将右边表达式值复制一份,交给左边的运算数.换而言 ...

  9. Pycharm用鼠标滚轮控制字体大小

    一.pycharm字体放大的设置 File —> setting —> Keymap —>在搜寻框中输入:increase —> Increase Font Size(双击) ...

  10. 微服务---Eureka注册中心(服务治理)

    在上一篇的初识SpringCloud微服务中,我们简单讲解到服务的提供者与消费者,当服务多了之后,会存在依赖与管理之间混乱的问题,以及需要对外暴露自己的地址,为了解决此等问题,我们学习Eureka注册 ...