循环

将事物循环执行

作用

思考:假如我有个⼥朋友,有⼀天我们闹⽭盾⽣⽓了,⼥朋友说:道歉,说 10 遍“媳妇⼉,我错了”。这个时候程序员会怎么做?

答:10 遍 print(‘媳妇⼉,我错了’)

思考:复制粘贴 10 次吗?

答:重复执⾏ 10 次⼀样的代码,程序中循环即可

循环的分类

Python 提供了 for 循环和 while 循环

循环类型

描述

while

在给定的判断条件为 true 时执行循环体,否则退出循环体。

for

重复执行语句

循环控制语句

控制语句

描述

break 语句

在语句块执行过程中终止循环,并且跳出整个循环

continue 语句

在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。

pass 语句

pass 是空语句,是为了保持程序结构的完整性。

while 循环

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务

语法

[object Object],[object Object]

应用一:输出 100 次媳妇,我错了

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

应用二:计算 1-100 的和

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

应用三:计算 1-100 偶数和

2+4+6+8+10+…

偶数即是和 2 取余结果为 0 的数字,可以加⼊条件语句判断是否为偶数,为偶数则累加

方法一

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

方法二

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

break 和 continue 语句

**案例:**有 10 辆汽车需要刷漆,一辆一辆刷,就相当于循环过程

**break:**当刷到第 5 辆,完成了当天的任务,刷漆的动作就要停止,这⾥就是 break 控制循环流程,即终⽌此循环

**continue:**我拿的是马自达的油漆,结果第 6 辆车是劳斯劳斯,所以直接跳过这辆,继续刷马自达,这⾥就是 continue 控制循环流程,即退出当前⼀次循环继⽽执⾏下⼀次循环代码

应用一:刷漆-break

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

应用二:刷漆-continue

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

while 循环嵌套

思考:和⼥朋友闹⽭盾⽣⽓了,⼥朋友说:道歉,说 10 遍“媳妇⼉,我错了”,还不够,还要做家务,而且持续三天,这个程序该怎么写?

一天

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

三天

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

执行流程

当内部循环执⾏完成之后,再执⾏下⼀次外部循环的条件判断。

python while 循环中计算循环的次数_前端