python学习随笔之流程控制---while
死循环:
1 #!/usr/bin/python 2 while 1: 3 print "hello"
设置条件跳出循环:
1 #!/usr/bin/python 2 x = "" #定义x为空字符,如果不定义会出现语法错误 3 while x != "q":#如果x不等于q为真,则循环,如果x不等于q为假,即x=q为真,则跳出循环,执行"ending" 4 print "hello" 5 x = raw_input("please input something ,q for quit: ") 6 else: 7 print "ending"
上述方法使用q退出,也可以使用回车退出
1 #!/usr/bin/python 2 x = "" 3 while x != "q":4 print "hello" 5 x = raw_input("please input something ,q for quit: ") 6 if not x: 7 break
8 if x == c:
9 continue
10 print "#"*50
11 else: 12 print "ending"
跳出循环则不显示ending
continue用法与for一样:
1 #!/usr/bin/python 2 x = "" 3 while x != "q": 4 print "hello" 5 x = raw_input("please input something ,q for quit: ") 6 if not x: 7 break 8 if x == "c": 9 continue 10 print "#"*50 11 else: 12 print "ending"
当输入a时:
hello
###################
当输入c时:
hello
无#显示,因为continue