Python中的while循環(huán)

while循環(huán)

while是python中執(zhí)行循環(huán)的一個重要方法.只要條件是true,while語句會不斷重復執(zhí)行下去.
while循環(huán)的基本語法如下:
while test:
code statements
else:
final code statements

實例

x = 0

while x < 10:
    print('x is currently: ',x)
    print(' x is still less than 10, adding 1 to x')
    x+=1

執(zhí)行結(jié)果:
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
請仔細看執(zhí)行結(jié)果進行了多少次打印以及在條件x == 10滿足前,while循環(huán)共進行了多少次.
我們在上面的代碼基礎(chǔ)上加上else

x = 0

while x < 10:
    print('x is currently: ',x)
    print(' x is still less than 10, adding 1 to x')
    x+=1
    
else:
    print('All Done!')

執(zhí)行結(jié)果:
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
All Done!

break, continue, pass

如果我們的實際問題需要解決多種條件.可以在循環(huán)里使用break, continue, pass解決問題.
break指終止正在進行的最近的循環(huán).
continue指回到最近的循環(huán)的開頭.
pass指什么都不做.
加上break, continue,while循環(huán)的語法如下:

while test:
code statement
if test:
break
if test:
continue
else:
breakcontinue可以放在循環(huán)體里的任何位置,但是我們經(jīng)常把它們放在嵌套if語句后面以在某種條件下執(zhí)行操作.
實例

x = 0

while x < 10:
    print('x is currently: ',x)
    print(' x is still less than 10, adding 1 to x')
    x+=1
    if x==3:
        print('x==3')
    else:
        print('continuing...')
        continue

執(zhí)行結(jié)果如下:
x is currently: 0
x is still less than 10, adding 1 to x
continuing...
x is currently: 1
x is still less than 10, adding 1 to x
continuing...
x is currently: 2
x is still less than 10, adding 1 to x
x==3
x is currently: 3
x is still less than 10, adding 1 to x
continuing...
x is currently: 4
x is still less than 10, adding 1 to x
continuing...
x is currently: 5
x is still less than 10, adding 1 to x
continuing...
x is currently: 6
x is still less than 10, adding 1 to x
continuing...
x is currently: 7
x is still less than 10, adding 1 to x
continuing...
x is currently: 8
x is still less than 10, adding 1 to x
continuing...
x is currently: 9
x is still less than 10, adding 1 to x
continuing...

x = 0

while x < 10:
    print('x is currently: ',x)
    print(' x is still less than 10, adding 1 to x')
    x+=1
    if x==3:
        print('Breaking because x==3')
        break
    else:
        print('continuing...')
        continue

輸出結(jié)果:

x is currently: 0
x is still less than 10, adding 1 to x
continuing...
x is currently: 1
x is still less than 10, adding 1 to x
continuing...
x is currently: 2
x is still less than 10, adding 1 to x
Breaking because x==3

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末微饥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子五嫂,更是在濱河造成了極大的恐慌,老刑警劉巖趾徽,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異翰守,居然都是意外死亡孵奶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門蜡峰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來了袁,“玉大人,你說我怎么就攤上這事湿颅≡芈蹋” “怎么了?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵油航,是天一觀的道長崭庸。 經(jīng)常有香客問我,道長谊囚,這世上最難降的妖魔是什么怕享? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮镰踏,結(jié)果婚禮上函筋,老公的妹妹穿的比我還像新娘。我一直安慰自己余境,他們只是感情好驻呐,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著芳来,像睡著了一般含末。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上即舌,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天佣盒,我揣著相機與錄音,去河邊找鬼顽聂。 笑死肥惭,一個胖子當著我的面吹牛贸弥,可吹牛的內(nèi)容都是我干的垫毙。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼俐巴,長吁一口氣:“原來是場噩夢啊……” “哼耀石!你這毒婦竟也來了牵囤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎揭鳞,沒想到半個月后炕贵,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡野崇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年称开,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乓梨。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡鳖轰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出督禽,到底是詐尸還是另有隱情脆霎,我是刑警寧澤总处,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布狈惫,位于F島的核電站,受9級特大地震影響鹦马,放射性物質(zhì)發(fā)生泄漏胧谈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一荸频、第九天 我趴在偏房一處隱蔽的房頂上張望菱肖。 院中可真熱鬧,春花似錦旭从、人聲如沸稳强。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽退疫。三九已至,卻和暖如春鸽素,著一層夾襖步出監(jiān)牢的瞬間褒繁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工馍忽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棒坏,地道東北人。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓遭笋,卻偏偏與公主長得像坝冕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子瓦呼,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容