<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 習題 33: While 循環 接下來是一個更在你意料之外的概念: while-loop``(while循環)。``while-loop 會一直執行它下面的代碼片段,直到它對應的布爾表達式為 False 時才會停下來。 等等,你還能跟得上這些術語吧?如果你的某一行是以 : (冒號, colon)結尾,那就意味著接下來的內容是一個新的代碼片段,新的代碼片段是需要被縮進的。只有將代碼用這樣的方式格式化,Python 才能知道你的目的。如果你不太明白這一點,就回去看看“if 語句”和“函數”的章節,直到你明白為止。 接下來的練習將訓練你的大腦去閱讀這些結構化的代碼。這和我們將布爾表達式燒錄到你的大腦中的過程有點類似。 回到 while 循環,它所作的和 if 語句類似,也是去檢查一個布爾表達式的真假,不一樣的是它下面的代碼片段不是只被執行一次,而是執行完后再調回到 while 所在的位置,如此重復進行,直到 while 表達式為 False 為止。 While 循環有一個問題,那就是有時它會永不結束。如果你的目的是循環到宇宙毀滅為止,那這樣也挺好的,不過其他的情況下你的循環總需要有一個結束點。 為了避免這樣的問題,你需要遵循下面的規定: 1. 盡量少用 while-loop,大部分時候 for-loop 是更好的選擇。 1. 重復檢查你的 while 語句,確定你測試的布爾表達式最終會變成 False 。 1. 如果不確定,就在 while-loop 的結尾打印出你要測試的值。看看它的變化。 在這節練習中,你將通過上面的三樣事情學會 while-loop : <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>i = 0&#13; numbers = []&#13; &#13; while i &lt; 6:&#13; print "At the top i is %d" % i&#13; numbers.append(i)&#13; &#13; i = i + 1&#13; print "Numbers now: ", numbers&#13; print "At the bottom i is %d" % i&#13; &#13; &#13; print "The numbers: "&#13; &#13; for num in numbers:&#13; print num&#13; </pre> </div> </td> </tr></tbody></table> ### 你應該看到的結果 ~~~ $ 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 換成一個變量。 1. 使用這個函數重寫你的腳本,并用不同的數字進行測試。 1. 為函數添加另外一個參數,這個參數用來定義第 8 行的加值 +1 ,這樣你就可以讓它任意加值了。 1. 再使用該函數重寫一遍這個腳本。看看效果如何。 1. 接下來使用 for-loop 和 range 把這個腳本再寫一遍。你還需要中間的加值操作嗎?如果你不去掉它,會有什么樣的結果? 很有可能你會碰到程序跑著停不下來了,這時你只要按著 CTRL 再敲 c (CTRL-c),這樣程序就會中斷下來了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看