Python While循環(huán)語(yǔ)句

2023-02-28 13:57 更新

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

while 判斷條件(condition):
    執(zhí)行語(yǔ)句(statements)……

執(zhí)行語(yǔ)句可以是單個(gè)語(yǔ)句或語(yǔ)句塊。判斷條件可以是任何表達(dá)式,任何非零、或非空(?null?)的值均為?True?。

當(dāng)判斷條件為假 ?False ?時(shí),循環(huán)結(jié)束。

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


Gif 演示 Python while 語(yǔ)句執(zhí)行過(guò)程




實(shí)例

#!/usr/bin/python
count = 0
while count < 9:
    print 'The count is:', count
    count = count + 1
print "Good bye!"

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

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 ?語(yǔ)句時(shí)還有另外兩個(gè)重要的命令 ?continue?,?break ?來(lái)跳過(guò)循環(huán),?continue? 用于跳過(guò)該次循環(huán),?break ?則是用于退出循環(huán),此外"判斷條件"還可以是個(gè)常量,表示循環(huán)必定成立,具體用法如下:

# coding=utf-8
#continue 和 break 用法
 
i = 1
while i < 10:   
    i += 1
    if i%2 > 0:     # 非雙數(shù)時(shí)跳過(guò)輸出
        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:     # 當(dāng)i大于10時(shí)跳出循環(huán)
        break

運(yùn)行結(jié)果為:

2

4

6

8

10

1

2

3

4

5

6

7

8

9

10


無(wú)限循環(huán)

如果條件判斷語(yǔ)句永遠(yuǎn)為 ?True?,循環(huán)將會(huì)無(wú)限的執(zhí)行下去,如下實(shí)例:

實(shí)例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
var = 1
while var == 1:  # 該條件永遠(yuǎn)為true,循環(huán)將無(wú)限執(zhí)行下去
   num = raw_input("Enter a number  :")
   print "You entered: ", num
 
print "Good bye!"
#該代碼在在線代碼測(cè)試展現(xiàn)效果不好,建議在本地環(huán)境運(yùn)行

以上實(shí)例輸出結(jié)果:

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

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


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

在 Python 中,?while … els?e 在循環(huán)條件為 ?False ?時(shí)執(zhí)行 ?else ?語(yǔ)句塊:

實(shí)例

#!/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"

以上實(shí)例輸出結(jié)果為:

 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


簡(jiǎn)單語(yǔ)句組

類(lèi)似 ?if ?語(yǔ)句的語(yǔ)法,如果你的 ?while ?循環(huán)體中只有一條語(yǔ)句,你可以將該語(yǔ)句與 ?while ?寫(xiě)在同一行中, 如下所示:

實(shí)例

#!/usr/bin/python
 
flag = 1
 
while (flag): print 'Given flag is really true!'
 
print "Good bye!"

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


應(yīng)用實(shí)例

Pyhton 去除字符串首尾的空格:

# coding=utf-8
#一個(gè)簡(jiǎn)單的使用while去除字符串前后空格的代碼
str = '   W3cschool     '
#打印沒(méi)有處理前的字符串
print str

#使用循環(huán)除去字符串前的空格
while str[:1] == ' ':
    str = str[1:]
#使用循環(huán)除去字符串后的空格
while str[-1:] == ' ':
    str = str[:-1]
#打印處理后的結(jié)果
print str


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)