Python While循環語句

Python 編程中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理須要重複處理的相同任務。其基本形式爲:python

while 判斷條件:
    執行語句……
執行語句能夠是單個語句或語句塊。判斷條件能夠是任何表達式,任何非零、或非空(null)的值均爲true。
當判斷條件假false時,循環結束。
執行流程圖以下:

Gif 演示 Python while 語句執行過程

實例:
1 #!/usr/bin/python
2 
3 count = 0
4 while (count < 9):    print 'The count is:', count    count = count + 1  print "Good bye!"
以上代碼執行輸出結果:
 1 The count is: 0
 2 The count is: 1
 3 The count is: 2
 4 The count is: 3
 5 The count is: 4
 6 The count is: 5
 7 The count is: 6
 8 The count is: 7
 9 The count is: 8
10 Good bye!

while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用於跳過該次循環,break 則是用於退出循環,此外"判斷條件"還能夠是個常值,表示循環一定成立,具體用法以下:編程

 1 # continue 和 break 用法
 2 
 3 i = 1
 4 while i < 10:        i += 1     if i%2 > 0:     # 非雙數時跳過輸出
 5         continue
 6     print i         # 輸出雙數二、四、六、八、10
 7 
 8 i = 1
 9 while 1:            # 循環條件爲1一定成立
10     print i         # 輸出1~10
11     i += 1
12     if i > 10:     # 當i大於10時跳出循環
13         break

無限循環

若是條件判斷語句永遠爲 true,循環將會無限的執行下去,以下實例:
1 #coding=utf-8
2 #!/usr/bin/python
3 
4 var = 1
5 while var == 1 :  # 該條件永遠爲true,循環將無限執行下去
6    num = raw_input("Enter a number  :")
7    print "You entered: ", num
8 
9 print "Good bye!"
以上實例輸出結果:
 1 Enter a number  :20
 2 You entered:  20
 3 Enter a number  :29
 4 You entered:  29
 5 Enter a number  :3
 6 You entered:  3
 7 Enter a number between :Traceback (most recent call last):
 8   File "test.py", line 5, in <module>
 9     num = raw_input("Enter a number :")
10 KeyboardInterrupt
注意:以上的無限循環你能夠使用 CTRL+C 來中斷循環。
 

循環使用 else 語句

 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是經過 break 跳出而中斷的)的狀況下執行,while … else 也是同樣。bash

1 #!/usr/bin/python
2 
3 count = 0
4 while count < 5:
5    print count, " is  less than 5"
6    count = count + 1
7 else:
8    print count, " is not less than 5"
以上實例輸出結果爲:
1 0 is less than 5
2 1 is less than 5
3 2 is less than 5
4 3 is less than 5
5 4 is less than 5
6 5 is not less than 5

簡單語句組

相似if語句的語法,若是你的while循環體中只有一條語句,你能夠將該語句與while寫在同一行中, 以下所示:
1 #!/usr/bin/python
2 
3 flag = 1
4 
5 while (flag): print 'Given flag is really true!'
6 
7 print "Good bye!"
注意:以上的無限循環你能夠使用 CTRL+C 來中斷循環。

While循環語句實例

猜拳小遊戲
 
 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 import random
 5 while 1:
 6     s = int(random.randint(1, 3))
 7     if s == 1:
 8         ind = "石頭"
 9     elif s == 2:
10         ind = "剪子"
11     elif s == 3:
12         ind = ""
13     m = raw_input('輸入 石頭、剪子、布,輸入"end"結束遊戲:')
14     blist = ['石頭', "剪子", ""]
15     if (m not in blist) and (m != 'end'):
16         print "輸入錯誤,請從新輸入!"
17     elif (m not in blist) and (m == 'end'):
18         print "\n遊戲退出中..."
19         break
20     elif m == ind :
21         print "電腦出了: " + ind + ",平局!"
22     elif (m == '石頭' and ind =='剪子') or (m == '剪子' and ind =='') or (m == '' and ind =='石頭'):
23         print "電腦出了: " + ind +",你贏了!"
24     elif (m == '石頭' and ind =='') or (m == '剪子' and ind =='石頭') or (m == '' and ind =='剪子'):
25         print "電腦出了: " + ind +",你輸了!"
 
以上實例輸出結果爲:
 
1 輸入 石頭、剪子、布,輸入"end"結束遊戲:石頭
2 電腦出了: 石頭,平局!
3 輸入 石頭、剪子、布,輸入"end"結束遊戲:石頭    
4 電腦出了: 剪子,你贏了!
5 輸入 石頭、剪子、布,輸入"end"結束遊戲:
更多精彩文章請關注公衆號python社區營其它相關文章
相關文章
相關標籤/搜索