5.Python第五课-循环语句.ppt
- 格式:ppt
- 大小:784.00 KB
- 文档页数:20
pythonwhile循环语句1.while 循环结构特征:减少代码冗余,提升代码效率语法:while 条件表达式:code1code2...1.初始化⼀个变量2.写上循环的条件表达式3.⾃增⾃减的变量值案例1:打印1~100i = 1while i<=100:# 要执⾏的逻辑 ...print(i)i += 1"""初始化⼀个变量 i第⼀次循环:i <= 100 条件成⽴,执⾏循环直接打印 print(i) => 1i += 1 => i = 2第⼆次循环:回到17⾏,重新回到判断,看⼀看是否满⾜条件2 <= 100 条件成⽴,执⾏循环直接打印 print(i) => 2i += 1 => i = 3第三次循环:回到17⾏,重新回到判断,看⼀看是否满⾜条件3 <= 100 条件成⽴,执⾏循环直接打印 print(i) => 3i += 1 => i = 4......依次类推进⾏循环 ...到什么时候结束?当打印100 结束之后i += 1 => i = 101 回到循环的判断条件中发现101 <= 100 不满⾜条件,不成⽴,不执⾏循环体代码块到此为⽌,循环直接结束."""案例2 : 打印1~100的累加和?1+2+3+4+ .....+100 ?#⽅法⼀total 负责做累加和total = 0i = 1while i<=100:total += ii+=1print(total)"""# 代码解析:第⼀次循环:1 <= 100 条件为真 , 执⾏循环total += i => total = total + i => total = 0 + 1i+=1 => i=2第⼆次循环:回到条件表达式中进⾏判断2<=100 条件为真 , 执⾏循环total += i => total = total + i => total = 0 + 1 + 2i+=1 => i=3回到条件表达式中进⾏判断3<=100 条件为真 , 执⾏循环total += i => total = total + i => total = 0 + 1 + 2 + 3i+=1 => i=4......依次循环什么时候结束?total = 0 + 1 + 2 + 3 + ... + 100i+=1 => i = 101回到条件表达式中进⾏判断101 <= 100 条件不成⽴,不执⾏循环体;直接终⽌循环;print(total) 5050"""# ⽅法⼆# 1+2+3+4+ .....+100 ? ⽤while True 的⽅式实现;total = 0i = 1sign = Truewhile sign:total += ii+=1# ⼿动加上终⽌的条件if i == 101:sign = Falseprint(total)# 这种情况下就不执⾏循环了;⽤这样的⽅式终⽌循环;while False:print(111)2.死循环while True:print(123)(1)单循环练习1.打印⼀⾏⼗个⼩星星i = 1while i <= 10:print("★",end="")i += 12.⽤⼀个变量打印出⼀⾏⼗个⼩星星i = 0strvar = ""while i<10:# 循环10次,拼接10个星星,每循环⼀次拼接⼀个星星strvar += "★"i+=1print(strvar)3.打印⼀⾏⼗个⼩星星奇数个打印★偶数个打印☆i = 1while i <= 10:if i % 2 == 0:print("☆",end='')else:print("★",end="")i += 1#⽅法⼆print("-----分割线-------")count = 1str_var = ""while count <= 10:if count % 2 == 0:str_var += "☆"else:str_var += "★"print(str_var)4.⼀个循环,打印⼗⾏⼗列⼩星星#循环100和星星i = 1while i <= 100:#1.打印⼩星星print("★",end='')#2.打印换⾏if i % 10 == 0:print()i += 15.⼀个循环打印⼗⾏⼗列隔列变⾊⼩星星#⽅法⼀i = 0while i < 100:if i % 2 == 0:print("☆",end="")else:print("★",end='')if i % 10 == 9:print()i += 1#⽅法⼆_str_var = ""count = 1while count <= 100:if count % 2 == 0:_str_var += "☆"else:_str_var += "★"if count % 10 == 0:_str_var +="\n"count += 1print(_str_var)6.⼀个循环打印⼗⾏⼗列隔⾏变⾊⼩星星i = 0while i < 100:#打印星星if i // 10 %2 == 0:print("☆", end="")else:print("★",end="")if i % 10 == 9:print()i += 11.pass 过 (当不能直接在代码块中写上具体代码时,先⽤pass占位;) num = 10if num == 10:passi = 0while i<10:passi+=12.break 终⽌当前循环,只能⽤在循环当中# 打印1~10 ,遇到5就终⽌循环# 单循环 (终⽌当前循环)i = 1while i<=10:if i == 5:breakprint(i)i+=1多循环 (终⽌当前循环)i = 0while i<3:j = 0while j<3:if j ==2:breakprint(i,j)j+=1i+=1# 不能再循环外使⽤break"""if 5==5:print(111)break"""3.continue 跳过当前循环,从下⼀次开始 ,只能⽤在循环当中打印1 ~ 10 不包括5i = 1while i<=10:if i == 5:# ⼿动加1 , 防⽌跳过下⾯代码,忽略⾃增形成死循环;i+=1continueprint(i)i+=1打印1~100 所有不含有4的数字;"""// 地板除可以取⼀个数的⾼位% 取余可以取⼀个数的低位任意数 // n 会产⽣n个相同的数字任意数 % n 取值范围是0~(n-1)"""# ⽅法⼀i = 1while i<=100:if i % 10 == 4 or i // 10 == 4:# ⼿动⾃增1,防⽌死循环i+=1continueprint(i)i+=1# ⽅法⼆ ini = 1while i<=100:strvar = str(i)if '4' in strvar:i+=1continueprint(i)i+=1(3) 双层循环练习1.⼗⾏⼗列⼩星星#j动1次,i动10次,外层循环动的慢.内层循环动的快j = 0while j<10:#打印星星i = 0while i < 10:print("*",end='')i += 1# 打印换⾏print()j += 12 双层⼗⾏⼗列隔列换⾊⼩星星j = 0while j<10:#打印⼀⾏⼗个星星i = 0while i < 10:if i % 2 == 0:print("☆",end='')else:print("★",end='')i += 1#打印⼀个换⾏print()j += 13双层⼗⾏⼗列隔⾏换⾊⼩星星"""当j = 0 时,在内存循环,循环了10次☆当j = 1 时,在内层循环,循环了10次★"""print("------分割线------")j = 0while j < 10:#打印⼀⾏⼗个⼩星星i = 0while i < 10:if j % 2 == 0:print("★",end='')else:print("☆",end='')i += 1#打印⼀个换⾏print()j += 14.打印99乘法表"""外层的i控制⾏⾥层的j控制列当前j跟当前⾏数有关系,不能⼤于当前⾏所以j <= i """#⽅法⼀(正序 1~9)i = 1while i <= 9:j = 1while j <= i:#字符串的格式化让最后的乘积占⽤2位,据右边显⽰; print("%s*%s =%2s"%(i,j,i*j),end=" ")j += 1print()i += 1"""'字符串'%(值1,值2,值3,......)%s 字符串占位符res = "%s * %s = ?"% (5,6)print(res)"""# ⽅法⼆(倒序 9 ~1)i = 9while i >= 1:#字符串的格式化让最后的乘积占⽤2位,据右边显⽰; j = 1while j <= i:#字符串的格式化让最后的乘积占⽤2位,据右边显⽰; print("%d*%d=%2d"% (i,j,i*j),end=' ')j += 1#打印换⾏print()i -= 1# ⽅法三i = 1while i <= 9:#(1)先打印空格k = 9 - iwhile k > 0:# """打印空格"""print(" ",end="")k -= 1#print(" "*(9-i),end="")#(2)打印表达式j = 1while j <= i:print("%d*%d=%2d"% (i,j,i*j),end="")j += 1#(3)打印换⾏print()i +=1print("-----分割线-----")#⽅法四i = 9while i > 0:k = 9 - iwhile k > 0:print(" ",end='')k -= 1# print(" "*(9-i),end='')# (2) 打印表达式j = 1while j <= i:#字符串的格式化让最后的乘积占⽤2位,据右边显⽰;print("%d*%d = %2d"% (i,j,i*j),end=' ')j += 1#打印换⾏print()i -= 15 求吉利数字100 ~ 999 666 888 111 222 333 444 ... 123 789 567 987 321 543 210 """// 地板除可以获取⼀个数的⾼位% 取余可以获取⼀个数的低位98 98 % 10 => 898 98 // 10 => 9678个位 = 678 % 10 => 8⼗位 = 678 // 10 %10 => 7百位 = 678 // 100 => 6"""#⽅法⼀i = 100while i <= 999:gewei = i % 10shiwei = i // 10 % 10baiwei = i // 100#gewei = shiwei = baiwei 三者相同if shiwei == gewei and shiwei ==baiwei:print(i)#678if shiwei == gewei -1 and shiwei == baiwei + 1:print(i)#876if shiwei == gewei + 1 and shiwei == baiwei - 1:print(i)i += 1#⽅法⼆字符串形式写print("-----分割线-------")i = 100while i<= 999:num = str(i)#"987"geiwei = int(num[-1]) #7shiwei = int(num[1]) #8baiwei = int(num[0]) #9if shiwei == gewei and shiwei == baiwei:print(i)if shiwei == gewei - 1 and shiwei == baiwei + 1:print(i)if shiwei == gewei + 1 and shiwei == baiwei - 1:print(i)i += 1经典题: 百钱买百鸡"""公鸡,母鸡,⼩鸡,公鸡1块钱⼀只,母鸡3块钱⼀只,⼩鸡是5⽑钱⼀只,问100块钱买100只鸡,有多少种买法穷举法: ⼀个⼀个拿出来试x [1,2]y [3,4]z [5,6]满⾜ x+y+z = 101 + 3 + 5 = 91 + 3 + 6 = 10 ok1 + 4 + 5 = 10 ok1 + 4 + 6 = 112 +3 + 5 = 10 ok2 +3 + 6 = 112 + 4 + 5 = 112 + 4 + 6 = 12"""#公鸡x只,母鸡y只,⼩鸡z只#x+y+z = 100 #⼀百只鸡#x+3+y*1+0.5*z = 100#两个条件同时满⾜x = 0total = 0while x<=100:y = 0while y<= 33:z = 0while z <= 100:if(x+y+z == 100) and (x+3*y+0.5*z == 100):total += 1z += 1y += 1x += 1print(total)。