资源预览内容
第1页 / 共62页
第2页 / 共62页
第3页 / 共62页
第4页 / 共62页
第5页 / 共62页
第6页 / 共62页
第7页 / 共62页
第8页 / 共62页
第9页 / 共62页
第10页 / 共62页
亲,该文档总共62页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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依次取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(Enter 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, 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循环的特点,循环前测试条件若不满足,则循环体一次都不执行循环体影响下一次条件测试否则导致无穷循环,条件,循环体,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 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”的输入烦死,改进:设置一个特殊数据值作为终止循环的信号。,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(): sum = 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) 45Enter 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,解决办法:把所有的输入信息作为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 sum / 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 .( 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,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号