Python While 循環語句
Python 編程中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理須要重複處理的相同任務。其基本形式爲:php
while 判斷條件: 執行語句……
執行語句能夠是單個語句或語句塊。判斷條件能夠是任何表達式,任何非零、或非空(null)的值均爲true。python
當判斷條件假false時,循環結束。編程
執行流程圖以下:app
Gif 演示 Python while 語句執行過程
實例
#!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
運行實例 »
以上代碼執行輸出結果:less
The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!
while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用於跳過該次循環,break 則是用於退出循環,此外"判斷條件"還能夠是個常值,表示循環一定成立,具體用法以下:dom
# continue 和 break 用法 i = 1 while i < 10: i += 1 if i%2 > 0: # 非雙數時跳過輸出 continue print i # 輸出雙數二、四、六、八、10 i = 1 while 1: # 循環條件爲1一定成立 print i # 輸出1~10 i += 1 if i > 10: # 當i大於10時跳出循環 break
無限循環
若是條件判斷語句永遠爲 true,循環將會無限的執行下去,以下實例: ide
實例
#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 該條件永遠爲true,循環將無限執行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"
以上實例輸出結果:oop
Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "test.py", line 5, in <module> num = raw_input("Enter a number :") KeyboardInterrupt
注意:以上的無限循環你能夠使用 CTRL+C 來中斷循環。測試
循環使用 else 語句
在 python 中,while … else 在循環條件爲 false 時執行 else 語句塊:ui
實例
#!/usr/bin/python
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
以上實例輸出結果爲:
0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less 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 -*- import random s = int(random.uniform(1,10)) #print(s) m = int(input('輸入整數:')) while m != s: if m > s: print('大了') m = int(input('輸入整數:')) if m < s: print('小了') m = int(input('輸入整數:')) if m == s: print('OK') break;
猜拳小遊戲
#!/usr/bin/python # -*- coding: UTF-8 -*- import random while 1: s = int(random.randint(1, 3)) if s == 1: ind = "石頭" elif s == 2: ind = "剪子" elif s == 3: ind = "布" m = raw_input('輸入 石頭、剪子、布,輸入"end"結束遊戲:') blist = ['石頭', "剪子", "布"] if (m not in blist) and (m != 'end'): print "輸入錯誤,請從新輸入!" elif (m not in blist) and (m == 'end'): print "\n遊戲退出中..." break elif m == ind : print "電腦出了: " + ind + ",平局!" elif (m == '石頭' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石頭'): 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 -*- import random import sys import time result = [] while True: result.append(int(random.uniform(1,7))) result.append(int(random.uniform(1,7))) result.append(int(random.uniform(1,7))) print result count = 0 index = 2 pointStr = "" while index >= 0: currPoint = result[index] count += currPoint index -= 1 pointStr += " " pointStr += str(currPoint) if count <= 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("輸入十進制數:") print denum,"(10)", binnum = [] # 二進制數 while denum > 0: binnum.append(str(denum % 2)) # 棧壓入 denum //= 2 print '= ', while len(binnum)>0: import sys sys.stdout.write(binnum.pop()) # 無空格輸出print ' (2)'
九九乘法表
690***907@qq.com
while循環 - 九九乘法表
#!/usr/bin/python # -*- coding: UTF-8 -*- #九九乘法表 i = 1 while i : j = 1 while j: print j ,"*", i ," = " , i * j , ' ', if i == j : break j += 1 if j >= 10: break print "\n" i += 1 if i >= 10: break