python第8章循环结构

上传人:飞*** 文档编号:5507361 上传时间:2017-08-07 格式:PPT 页数:62 大小:716KB
返回 下载 相关 举报
python第8章循环结构_第1页
第1页 / 共62页
python第8章循环结构_第2页
第2页 / 共62页
python第8章循环结构_第3页
第3页 / 共62页
python第8章循环结构_第4页
第4页 / 共62页
python第8章循环结构_第5页
第5页 / 共62页
点击查看更多>>
资源描述

《python第8章循环结构》由会员分享,可在线阅读,更多相关《python第8章循环结构(62页珍藏版)》请在金锄头文库上搜索。

1、1,第8章,控制结构II循环,2,Objectives,for和while循环的区别和应用范围 了解几种循环模式:interactive loop、sentinel loop、EOF loop、nest loop.Bool代数和bool计算,3,Outline, 8.1 for 循环: 回顾 8.2 不确定循环 8.3 常见循环模式 8.4 布尔值计算 8.5 其他循环结构,4,for 循环:review,The for statement allows us to iterate through a sequence of values.语法: for in : 语义:循环索引值var依次取

2、sequence中的值,每循环一次执行一次,5,假设我们要求一组数的均值为了实现程序的通用性,必须使得该程序能够求任意个数的平均值程序特点:不要求记录每个值,只要求平均值,所以我们可以只记录加和值。,for 循环,6,# average1.py# A program to average a set of numbers# Illustrates counted loop with accumulatordef main(): n = input(How many numbers do you have? ) sum = 0.0 for i in range(n): x = input(Ent

3、er a number ) sum = sum + x print nThe average is, sum / n,for 循环,7,How many numbers do you have? 5Enter a number 32Enter a number 45Enter a number 34Enter a number 76Enter a number 45The average of the numbers is 46.4,8,average1.py的缺点:需要用户输入n,不适合事先不知道n的场合不知道n则不能用确定的计数循环for不确定的条件循环:while,9,Outline,

4、8.1 for 循环: 回顾 8.2 不确定循环 8.3 常见循环模式 8.4 布尔值计算 8.5 其他循环结构,10,语法:while : nextstatement语义: 只要条件成立就反复执行循环体; 当条件不成立则执行下一条语句.,不确定循环:while,11,while : 条件表达式(condition)是一个bool型的表达式。语句体(body)是一句或多句语句,多句语句时缩进对齐。每次循环时,先检查条件表达式,为真时执行body,为假时略过body执行while后的语句,循环终止。,不确定循环:while,12,while循环的特点,循环前测试条件若不满足,则循环体一次都不执行

5、循环体影响下一次条件测试否则导致无穷循环,条件,循环体,yes,no,13,例如:要依次打印110这10个数:for loop:for i in range(11): print iwhile loop:i = 0while i ) sum = sum + x count = count + 1 moredata = raw_input(.(yes or no)? ) print nThe average is, sum / count,19,运行average2.py,Enter a number 32Do you have more numbers (yes or no)? yEnter

6、a number 45Do you have more numbers (yes or no)? yesEnter a number 34Do you have more numbers (yes or no)? yupEnter a number 76Do you have more numbers (yes or no)? yEnter a number 45Do you have more numbers (yes or no)? nahThe average of the numbers is 46.4,优点:用户不用事先输入n缺点:用户会被没完没了的“y”的输入烦死,改进:设置一个特

7、殊数据值作为终止循环的信号。,20,(二)Sentinel Loops,A sentinel loop 就是一直循环干事直到碰到一个特殊值。这个特殊值被称为哨兵(sentinel)。这个哨兵必须能很明确地和其它输入值分开。例如,如果我们的求平均例子求的是学生的分数,那么可以设小于0的数为sentinel.,21,(二)Sentinel Loops,# average3.py# A program to average a set of numbers# Illustrates sentinel loop using negative input as sentineldef main(): s

8、um = 0.0 count = 0 x = input(Enter a number (negative to quit) ) while x = 0: sum = sum + x count = count + 1 x = input(Enter a number (negative to quit) ) print nThe average of the numbers is, sum / count,22,执行Sentinel Loops,Enter a number (negative to quit) 32Enter a number (negative to quit) 45En

9、ter a number (negative to quit) 34Enter a number (negative to quit) 76Enter a number (negative to quit) 45Enter a number (negative to quit) -1The average of the numbers is 46.4,23,Sentinel Loops,哨兵循环能完成交互式输入且不用频繁输入y。average3.py的缺点:哨兵很难找,例子中不能输入负数求平均值。要想实现所有数都能在一起输入求平均值,哨兵得换一个。,24,Sentinel Loops,解决办法

10、:把所有的输入信息作为string.有效的输入转换成数字用于求和,输入非数字时视为哨兵。We could use the empty string (“”)!,25,Sentinel Loops,initialize sum to 0.0initialize count to 0input data item as a string, xStrwhile xStr is not empty convert xStr to a number, x add x to sum add 1 to count input next data item as a string, xStrOutput su

11、m / count,26,Sentinel Loops,# average4.py# A program to average a set of numbers# Illustrates sentinel loop using empty string as sentineldef main(): sum = 0.0 count = 0 xStr = raw_input(Enter a number( to quit) ) while xStr != : x = eval(xStr) sum = sum + x count = count + 1 xStr = raw_input(Enter

12、.( to quit) ) print nThe average of the numbers is, sum / count,27,执行average4.py,Enter a number ( to quit) 34Enter a number ( to quit) 23Enter a number ( to quit) 0Enter a number ( to quit) -25Enter a number ( to quit) -34.4Enter a number ( to quit) 22.7Enter a number ( to quit) The average of the numbers is 3.38333333333,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号