新聞詳情
python ——2.2 while循環(huán)語句發(fā)表時(shí)間:2022-05-09 16:18 Python 編程中 while 語句用于循環(huán)執(zhí)行程序,即在某條件下,循環(huán)執(zhí)行某段程序,以處理需要重復(fù)處理的相同任務(wù)。其基本形式為: while 判斷條件(condition): 執(zhí)行語句(statements)…… 執(zhí)行語句可以是單個(gè)語句或語句塊。判斷條件可以是任何表達(dá)式,任何非零、或非空(null)的值均為true。 當(dāng)判斷條件假 false 時(shí),循環(huán)結(jié)束。 執(zhí)行流程圖如下: 實(shí)例 #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" 運(yùn)行實(shí)例 ? 以上代碼執(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! |