<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 練習33.while循環 接下來是一個更在你意料之外的概念:`while循環(while-loop)`。while循環會一直執行它下面的代碼片段,直到它對應的布爾表達式為`False`時才會停下來。 你還能跟得上這些術語吧?如果你的某一行是以`:`(冒號)結尾,那就意味著接下來的內容是一個新的代碼片段,新的代碼片段是需要縮進的。只有將代碼用這樣的方式格式化,Python才能知道你的目的。如果你不太明白這一點,就回去看看“if 語句”和“函數”的章節,直到你明白為止。 接下來的練習將訓練你的大腦去閱讀這些結構化的代碼。這和我們將布爾表達式燒錄到你的大腦中的過程有點類似。 回到`while` 循環,它所作的和`if`語句類似,也是去檢查一個布爾表達式的真假,不一樣的是它下面的代碼片段不是只被執行一次,而是執行完后再調回到 `while` 所在的位置,如此重復進行,直到`while`表達式為`False`為止。 `While` 循環有一個問題,那就是有時它永遠不會結束。如果你的目的是循環到宇宙毀滅為止,那這樣也挺好的,不過其他的情況下你的循環總需要有一個結束。 為了避免這樣的問題,你需要遵循下面的規定: > 1. 盡量少用`while-loop`,大部分時候`for-loop`是更好的選擇。 > 1. 重復檢查你的`while`語句,確定你的布爾表達式最終會變成`False` 。 > 1. 如果不確定,就在while循環的結尾打印出你測試的值。看看它的變化。 在這節練習中,你將通過上面的三個檢查學會`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 ~~~ ### 你看到的結果 ~~~ $ 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),這樣程序就會中斷下來了。 ## 常見問題 ### Q: for 循環和while循環有什么區別? > for 循環只能對某種事物的集合做循環,而while可以進行任何種類的循環。但是,while循環很容易出錯,大部分情況for循環也是一個很好的選擇。 ### Q: 循環好難啊,我怎么才能掌握它? > 人們不理解循環的主要原因是因為他們不理解代碼的“跳躍性”。當一個循環運行的時候,它會執行完循環的代碼塊,然后從代碼塊的末尾跳到開頭。想象一下,在循環中放一些打印語句,當Python運行的時候,看一下變量在這些位置是如何變化的。把打印語句寫在循環之前,循環的開頭,循環的中間,以及循環結束的位置,研究一下這些輸出,再試著理解一下代碼是如何跳躍的。
                  <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>

                              哎呀哎呀视频在线观看