日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Pythonwhile循環(huán)

Python 使用while和作為關(guān)鍵字來(lái)構(gòu)成一個(gè)條件循環(huán),通過(guò)這個(gè)循環(huán)重復(fù)執(zhí)行一個(gè)語(yǔ)句塊,直到指定的布爾表達(dá)式為真。

以下是 while循環(huán)語(yǔ)法。

Syntax:

while [boolean expression]:
    statement1
    statement2
    ...
    statementN

Python 關(guān)鍵字 while 有一個(gè)條件表達(dá)式,后跟:符號(hào),以增加縮進(jìn)開(kāi)始一個(gè)塊。 該塊有要重復(fù)執(zhí)行的語(yǔ)句。這樣的塊通常被稱為循環(huán)體。身體將繼續(xù)執(zhí)行,直到情況評(píng)估為True。如果結(jié)果是False,程序?qū)⑼顺鲅h(huán)。 以下示例演示了 while循環(huán)。

Example: while loop

num =0

while num < 5:
    num = num + 1
    print('num = ', num) 

Output

num = 1
num = 2
num = 3
num = 4
num = 5

在這里,while 語(yǔ)句之后的重復(fù)塊包括遞增一個(gè)整型變量的值并打印它。在塊開(kāi)始之前,變量 num 被初始化為 0。直到它小于 5,num 遞增 1 并打印出來(lái)以顯示數(shù)字序列,如上所示。

循環(huán)體中的所有語(yǔ)句必須以相同的縮進(jìn)開(kāi)始,否則會(huì)引發(fā)IndentationError。

Example: Invalid Indentation

num =0
while num < 5:
    num = num + 1
      print('num = ', num) 

Output

 print('num = ', num)
  ^
IndentationError: unexpected indent

退出 while循環(huán)

在某些情況下,使用break關(guān)鍵字退出 while循環(huán)。使用 if 條件確定何時(shí)退出 while循環(huán),如下所示。

Example: Breaking while loop

num = 0

while num < 5:
    num += 1   # num += 1 is same as num = num + 1
    print('num = ', num)
    if num == 3: # condition before exiting a loop
        break 

Output

num = 1
num = 2
num = 3 

繼續(xù)下一次迭代

使用continue關(guān)鍵字開(kāi)始下一次迭代,在某些條件下跳過(guò)continue語(yǔ)句之后的語(yǔ)句,如下所示。

Example: Continue in while loop

num = 0

while num < 5:
    num += 1   # num += 1 is same as num = num + 1
    if num > 3: # condition before exiting a loop
        continue
    print('num = ', num) 

Output

num = 1
num = 2
num = 3 

同時(shí)用其他塊循環(huán)

else塊可以跟隨while循環(huán)。當(dāng)while循環(huán)的布爾表達(dá)式計(jì)算為False時(shí),將執(zhí)行 else 塊。

使用continue關(guān)鍵字開(kāi)始下一次迭代,在某些條件下跳過(guò)continue語(yǔ)句之后的語(yǔ)句,如下所示。

Example: while loop with else block

num = 0

while num < 3:
    num += 1   # num += 1 is same as num = num + 1
    print('num = ', num)
else:
    print('else block executed') 

Output

num = 1
num = 2
num = 3
else block executed 

下面的 Python 程序連續(xù)地從用戶那里獲取一個(gè)數(shù)字作為輸入,并計(jì)算平均值,只要用戶輸入一個(gè)正數(shù)。這里,重復(fù)塊(循環(huán)的主體)要求用戶輸入一個(gè)數(shù)字,累計(jì)相加,如果不是負(fù)數(shù),則保持計(jì)數(shù)。

Example: while loop

num=0
count=0
sum=0

while num>=0:
    num = int(input('enter any number .. -1 to exit: '))
    if num >= 0:
        count = count + 1 # this counts number of inputs
        sum = sum + num # this adds input number cumulatively.
avg = sum/count
print('Total numbers: ', count, ', Average: ', avg) 

當(dāng)用戶提供負(fù)數(shù)時(shí),循環(huán)終止并顯示到目前為止提供的數(shù)字的平均值。下面是上述代碼的運(yùn)行示例:

Output

enter any number .. -1 to exit: 10
enter any number .. -1 to exit: 20
enter any number .. -1 to exit: 30
enter any number .. -1 to exit: -1
Total numbers: 3, Average: 20.0 

文章標(biāo)題:Pythonwhile循環(huán)
URL網(wǎng)址:http://m.5511xx.com/article/cciddgi.html