在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ exercise33.while 循環(huán)
附錄 A-練習(xí) 9:生成一個空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動文件 (mv)
exercise46.項目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號
exercise47.自動化測試
exercise3.數(shù)字和數(shù)學(xué)計算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達(dá)式
附錄 A-練習(xí) 4:創(chuàng)建一個路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會說面向?qū)ο?/span>
exercise52.開始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來制作一個游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進(jìn)行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設(shè)計和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計
附錄 A-簡介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise33.while 循環(huán)

接下來是一個更在你意料之外的概念:while 循環(huán)(while-loop)。while 循環(huán)會一直執(zhí)行它下面的代碼片段,直到它對應(yīng)的布爾表達(dá)式為 False 時才會停下來。

你還能跟得上這些術(shù)語吧?如果你的某一行是以:(冒號)結(jié)尾,那就意味著接下來的內(nèi)容是一個新的代碼片段,新的代碼片段是需要縮進(jìn)的。只有將代碼用這樣的方式格式化,Python 才能知道你的目的。如果你不太明白這一點,就回去看看“if 語句”和“函數(shù)”的章節(jié),直到你明白為止。

接下來的練習(xí)將訓(xùn)練你的大腦去閱讀這些結(jié)構(gòu)化的代碼。這和我們將布爾表達(dá)式燒錄到你的大腦中的過程有點類似。

回到 while 循環(huán),它所作的和 if 語句類似,也是去檢查一個布爾表達(dá)式的真假,不一樣的是它下面的代碼片段不是只被執(zhí)行一次,而是執(zhí)行完后再調(diào)回到 while 所在的位置,如此重復(fù)進(jìn)行,直到 while 表達(dá)式為 False 為止。

While 循環(huán)有一個問題,那就是有時它永遠(yuǎn)不會結(jié)束。如果你的目的是循環(huán)到宇宙毀滅為止,那這樣也挺好的,不過其他的情況下你的循環(huán)總需要有一個結(jié)束。

為了避免這樣的問題,你需要遵循下面的規(guī)定:

1.盡量少用 while-loop,大部分時候 for-loop 是更好的選擇。 2.重復(fù)檢查你的 while 語句,確定你的布爾表達(dá)式最終會變成 False 。 3.如果不確定,就在 while 循環(huán)的結(jié)尾打印出你測試的值。看看它的變化。

在這節(jié)練習(xí)中,你將通過上面的三個檢查學(xué)會 while-loop :

i = 0
numbers = []

while i < 6:
    print "At the top i is %d" % i
    numbers.append(i)

    i = i + 1
    print "Numbers now: ", numbers
    print "At the bottom i is %d" % i

print "The numbers: "

for num in numbers:
    print num

你看到的結(jié)果

$ python ex33.py
At the top i is 0
Numbers now:  [0]
At the bottom i is 1
At the top i is 1
Numbers now:  [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now:  [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now:  [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now:  [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now:  [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers: 
0
1
2
3
4
5

附加題

1.將這個 while 循環(huán)改成一個函數(shù),將測試條件(i < 6)中的 6 換成一個變量。 2.使用這個函數(shù)重寫你的腳本,并用不同的數(shù)字進(jìn)行測試。 3.為函數(shù)添加另外一個參數(shù),這個參數(shù)用來定義第 8 行的加值 +1 ,這樣你就可以讓它加任意值了。 4.再使用該函數(shù)重寫一遍這個腳本??纯葱Ч绾?。 5.使用 for-loop 和 range 把這個腳本再寫一遍。你還需要中間的加值操作嗎?如果不去掉它,會有什么樣的結(jié)果?

很有可能你會碰到程序跑著停不下來了,這時你只要按著 CTRL 再敲 c (CTRL-c),這樣程序就會中斷下來了。

常見問題

Q: for 循環(huán)和 while 循環(huán)有什么區(qū)別?

for 循環(huán)只能對某種事物的集合做循環(huán),而 while 可以進(jìn)行任何種類的循環(huán)。但是,while 循環(huán)很容易出錯,大部分情況 for 循環(huán)也是一個很好的選擇。

Q: 循環(huán)好難啊,我怎么才能掌握它?

人們不理解循環(huán)的主要原因是因為他們不理解代碼的“跳躍性”。當(dāng)一個循環(huán)運行的時候,它會執(zhí)行完循環(huán)的代碼塊,然后從代碼塊的末尾跳到開頭。想象一下,在循環(huán)中放一些打印語句,當(dāng) Python 運行的時候,看一下變量在這些位置是如何變化的。把打印語句寫在循環(huán)之前,循環(huán)的開頭,循環(huán)的中間,以及循環(huán)結(jié)束的位置,研究一下這些輸出,再試著理解一下代碼是如何跳躍的。