日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pythonwhile循环语句_Python While循环语句

發(fā)布時間:2024/3/13 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythonwhile循环语句_Python While循环语句 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python 編程中 while 語句用于循環(huán)執(zhí)行程序,即在某條件下,循環(huán)執(zhí)行某段程序,以處理需要重復處理的相同任務。其基本形式為:

while 判斷條件:

執(zhí)行語句……

執(zhí)行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。

當判斷條件假false時,循環(huán)結束。

執(zhí)行流程圖如下:

Gif 演示 Python while 語句執(zhí)行過程

實例:

#!/usr/bin/python

count = 0

while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"

以上代碼執(zhí)行輸出結果:

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 來跳過循環(huán),continue 用于跳過該次循環(huán),break 則是用于退出循環(huán),此外"判斷條件"還可以是個常值,表示循環(huán)必定成立,具體用法如下:

# continue 和 break 用法

i = 1

while i < 10: i += 1 if i%2 > 0: # 非雙數(shù)時跳過輸出

continue

print i # 輸出雙數(shù)2、4、6、8、10

i = 1

while 1: # 循環(huán)條件為1必定成立

print i # 輸出1~10

i += 1

if i > 10: # 當i大于10時跳出循環(huán)

break

無限循環(huán)

如果條件判斷語句永遠為 true,循環(huán)將會無限的執(zhí)行下去,如下實例:

#coding=utf-8

#!/usr/bin/python

var = 1

while var == 1 : # 該條件永遠為true,循環(huán)將無限執(zhí)行下去

num = raw_input("Enter a number :")

print "You entered: ", num

print "Good bye!"

以上實例輸出結果:

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

num = raw_input("Enter a number :")

KeyboardInterrupt

注意:以上的無限循環(huán)你可以使用 CTRL+C 來中斷循環(huán)。

循環(huán)使用 else 語句

在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區(qū)別,else 中的語句會在循環(huán)正常執(zhí)行完(即 for 不是通過 break 跳出而中斷的)的情況下執(zhí)行,while … else 也是一樣。

#!/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循環(huán)體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示:

#!/usr/bin/python

flag = 1

while (flag): print 'Given flag is really true!'

print "Good bye!"

注意:以上的無限循環(huán)你可以使用 CTRL+C 來中斷循環(huán)。

While循環(huán)語句實例

猜拳小游戲

#!/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"結束游戲:

python開發(fā)IT交流群:887934385分享源碼等相關資料

更多精彩文章請關注公眾號python社區(qū)營其它相關文章

總結

以上是生活随笔為你收集整理的pythonwhile循环语句_Python While循环语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。