pythonwhile⽆限循环语句_PythonWhile循环语句
Python While 循环语句
Python 编程中 while 语句⽤于循环执⾏程序,即在某条件下,循环执⾏某段程序,以处理需要重复处理的相同任务。其基本形式为:while判断条件:执⾏语句……
执⾏语句可以是单个语句或语句块。判断条件可以是任何表达式,任何⾮零、或⾮空(null)的值均为true。
当判断条件假false时,循环结束。
执⾏流程图如下:
Gif 演⽰ Python while 语句执⾏过程
实例
#!/usr/bin/pythoncount= 0while(count< 9): print'The count is:', countcount= count+ 1print"Good bye!"
运⾏实例 »while语句简单例子
以上代码执⾏输出结果:
Thecount is:0Thecount is:1Thecount is:2Thecount is:3Thecount is:4Thecount is:5Thecount is:6Thecount is:7Thecount is:8Goodbye!
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue ⽤于跳过该次循环,break 则是⽤于退出循环,此外"判断条件"还可以是个常值,表⽰循环必定成⽴,具体⽤法如下:
# continue 和 break ⽤法i= 1whilei< 10: i+= 1ifi%2> 0: # ⾮双数时跳过输出continueprinti# 输出双数2、4、6、8、10i= 1while1: # 循环条件为1必定成⽴printi# 输出1~10i+= 1ifi> 10: # 当i⼤于10时跳出循环break
⽆限循环
如果条件判断语句永远为 true,循环将会⽆限的执⾏下去,如下实例:
实例
#!/usr/bin/python# -*- coding: UTF-8 -*-var= 1whilevar== 1: # 该条件永远为true,循环将⽆限执⾏下去num= raw_input("Enter a number :")print"You entered: ", numprint"Good bye!"
以上实例输出结果:
Entera number :20Youentered:20Entera number :29Youentered:29Entera number :3Youentered:3Entera number between
:Traceback(most recent call last):File"test.py",line 5,innum =raw_input("Enter a number :")KeyboardInterrupt
注意:以上的⽆限循环你可以使⽤ CTRL+C 来中断循环。
循环使⽤ else 语句
在 python 中,while … else 在循环条件为 false 时执⾏ else 语句块:
实例
#!/usr/bin/python
count= 0
whilecount< 5:
printcount, "is less than 5"
count= count+ 1
else:
printcount, "is not less than 5"
以上实例输出结果为:
0isless than 51isless than 52isless than 53isless than 54isless than 55isnotless than 5
简单语句组
类似 if 语句的语法,如果你的 while 循环体中只有⼀条语句,你可以将该语句与while写在同⼀⾏中,
如下所⽰:
实例
#!/usr/bin/python
flag= 1
while(flag):print'Given flag is really true!'
print"Good bye!"
注意:以上的⽆限循环你可以使⽤ CTRL+C 来中断循环。
猜⼤⼩的游戏
#!/usr/bin/python# -*- coding: UTF-8 -*-importrandom
s =int(random.uniform(1,10))#print(s)m =int(input('输⼊整数:'))whilem !=s:ifm >s:print('⼤了')m =int(input('输⼊整数:'))ifm
猜拳⼩游戏
#!/usr/bin/python# -*- coding: UTF-8 -*-importrandom
while1:s =int(random.randint(1,3))ifs ==1:ind ="⽯头"elifs ==2:ind ="剪⼦"elifs ==3:ind ="布"m =raw_input('输⼊ ⽯头、剪⼦、
布,输⼊"end"结束游戏:')blist =['⽯头',"剪⼦","布"]if(m notinblist)and(m !='end'):print"输⼊错误,请重新输⼊!"elif(m
notinblist)and(m =='end'):print"\n游戏退出中..."breakelifm ==ind :print"电脑出了: "+ind +",平局!"elif(m =='⽯头'andind =='剪
⼦')or(m =='剪⼦'andind =='布')or(m =='布'andind =='⽯头'):print"电脑出了: "+ind +",你赢了!"elif(m == '⽯头' and ind =='布')
or (m == '剪⼦' and ind =='⽯头') or (m == '布' and ind =='剪⼦'):
print "电脑出了: " + ind +",你输了!"
测试结果:
输⼊⽯头、剪⼦、布,输⼊"end"结束游戏:⽯头电脑出了:⽯头,平局!输⼊⽯头、剪⼦、布,输⼊"end"结束游戏:⽯头电脑出了:剪⼦,你赢
了!输⼊⽯头、剪⼦、布,输⼊"end"结束游戏:
摇筛⼦游戏
#!/usr/bin/env python3# -*- coding: utf-8 -*-importrandom
importsys
importtime
result =
[]whileTrue:result.append(int(random.uniform(1,7)))result.append(int(random.uniform(1,7)))result.append(int(random.uniform(1,7))
count =0index =2pointStr =""whileindex >=0:currPoint =result[index]count +=currPoint
index -=1pointStr +=" "pointStr +=str(currPoint)ifcount <=11:sys.stdout.write(pointStr +" -> "+"⼩"+"\n")time.sleep(1)# 睡眠
⼀秒else:sys.stdout.write(pointStr +" -> "+"⼤"+"\n")time.sleep(1)# 睡眠⼀秒result =[]
⼗进制转⼆进制
#!/usr/bin/python# -*- coding: UTF-8 -*-denum =input("输⼊⼗进制数:")printdenum,"(10)",binnum =[]# ⼆进制数whiledenum
>0:binnum.append(str(denum %2))# 栈压⼊denum //= 2print'= ',whilelen(binnum)>0:importsys
sys.stdout.write(binnum.pop())# ⽆空格输出print ' (2)'
九九乘法表
690***907@qq
while循环 - 九九乘法表
#!/usr/bin/python# -*- coding: UTF-8 -*-#九九乘法表i =1whilei :j =1whilej:printj ,"*",i ," = ",i *j ,' ',ifi ==j :breakj +=1ifj
>=10:breakprint"\n"i +=1ifi >=10:break
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论