<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            習題 33: While 循環?

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

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

            接下來的練習將訓練你的大腦去閱讀這些結構化的代碼。這和我們將布爾表達式燒錄到你的大腦中的過程有點類似。

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

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

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

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

            在這節練習中,你將通過上面的三樣事情學會 while-loop

             1
             2
             3
             4
             5
             6
             7
             8
             9
            10
            11
            12
            13
            14
            15
            16
            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
            

            你應該看到的結果?

            $ 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 循環改成一個函數,將測試條件(i < 6)中的 6 換成一個變量。
            2. 使用這個函數重寫你的腳本,并用不同的數字進行測試。
            3. 為函數添加另外一個參數,這個參數用來定義第 8 行的加值 + 1 ,這樣你就可以讓它任意加值了。
            4. 再使用該函數重寫一遍這個腳本。看看效果如何。
            5. 接下來使用 for-looprange 把這個腳本再寫一遍。你還需要中間的加值操作嗎?如果你不去掉它,會有什么樣的結果?

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

            Project Versions

            Table Of Contents

            Previous topic

            習題 32: 循環和列表

            Next topic

            習題 34: 訪問列表的元素

            This Page

            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线