python 基礎 2.4 while 循環
阿新 • • 發佈:2017-10-25
nbsp else 打印 int utf code 循環 put 判斷
#/usr/bin/python
#coding=utf-8
#@Time :2017/10/18 15:31
#@Auther :liuzhenchuan
#@File :while 循環.py
示例1:
1 n = 0 2 while True: 3 print ‘hello world‘ 4 if n == 10: 5 break 6 n +=1
打印如下:
1 hello world 2 hello world 3 hello world 4 hello world 5 hello world 6 hello world 7 hello world8 hello world 9 hello world 10 hello world 11 hello world
示例2:
1 n = 0 2 while True: 3 if n==10: 4 break 5 print n ,‘hello‘ 6 n +=1
打印如下:
1 0 hello 2 1 hello 3 2 hello 4 3 hello 5 4 hello 6 5 hello 7 6 hello 8 7 hello 9 8 hello 10 9 hello
示例3:
#條件為假的時候,也是退出循環.輸入空字符的時候也退出循環
1 rinput = ‘‘ 2 while rinput !=‘q‘: 3 rinput = raw_input(‘please input something,q is quite: ‘) 4 print ‘hello‘ 5 if rinput == ‘‘: 6 break
示例4:
#應用邏輯非判斷,退出while循環。while條件為假時,退出while循環
1 rinput = ‘‘ 2 while rinput !=‘q‘: 3 rinput = raw_input(‘please input something,q is quite:‘) 4 print ‘hello‘ 5 if not rinput : 6 break
示例5:
# while...else...:正常結束while循環時會打印else後面的語句.與for...else...循環體一樣
1 rinput = ‘‘ 2 while rinput !=‘q‘: 3 rinput = raw_input(‘please input something,q is quite: ‘) 4 print ‘hello‘ 5 if not rinput : 6 break 7 else: 8 print ‘hello world‘
示例6:
#while 循環嵌套if循環體,支持contine
1 rinput = ‘‘ 2 while rinput !=‘q‘: 3 rinput = raw_input(‘please input something,q is quite: ‘) 4 print ‘hello‘ 5 if not rinput : 6 break 7 elif rinput == ‘quite‘: 8 continue 9 print ‘continue‘ 10 else: 11 print ‘hello world‘
python 基礎 2.4 while 循環