標籤:應用 支援 something enc lease input code pre 2.4
#/usr/bin/python
#coding=utf-8
#@Time :2017/10/18 15:31
#@Auther :liuzhenchuan
#@File :while 迴圈.py
樣本1:
1 n = 02 while True:3 print ‘hello world‘4 if n == 10:5 break6 n +=1
列印如下:
1 hello world 2 hello world 3 hello world 4 hello world 5 hello world 6 hello world 7 hello world 8 hello world 9 hello world10 hello world11 hello world
樣本2:
1 n = 02 while True:3 if n==10:4 break5 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 hello10 9 hello
樣本3:
#條件為假的時候,也是退出迴圈.輸入Null 字元的時候也退出迴圈
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 break7 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 迴圈