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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `While`循環 > 原文: [https://www.programiz.com/python-programming/while-loop](https://www.programiz.com/python-programming/while-loop) #### 循環在編程中用于重復特定的代碼塊。 在本文中,您將學習在 Python 中創建`while`循環。 ## 什么是 Python 中的`while`循環? 只要測試表達式(條件)為真,Python 中的`while`循環就可以迭代代碼塊。 當我們不知道事先進行迭代的次數時,通常使用此循環。 ### Python 中`while`循環的語法 ```py while test_expression: Body of while ``` 在`while`循環中,首先檢查測試表達式。 僅當`test_expression`求值為`True`時,才進入循環的主體。 一次迭代后,再次檢查測試表達式。 該過程一直持續到`test_expression`求值為`False`為止。 在 Python 中,`while`循環的主體是通過縮進確定的。 主體以縮進開始,第一條未縮進的線標記結束。 Python 將任何非零值解釋為`True`。`None`和`0`解釋為`False`。 ### `While`循環流程圖 ![while Loop in Python programming](https://img.kancloud.cn/42/c2/42c2e2f2a10ae84da764063e9cf6f316_223x372.png "while Loop Flowchart") Python 中`while`循環的流程圖 ### 示例:Python `while`循環 ```py # Program to add natural # numbers up to # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum) ``` 運行該程序時,輸出為: ```py Enter n: 10 The sum is 55 ``` 在上述程序中,只要我們的計數器變量`i`小于或等于`n`(在我們的程序中為 10),則測試表達式為`True`。 我們需要在循環體內增加計數器變量的值。 這是非常重要的(并且幾乎被遺忘了)。 否則,將導致無限循環(永無止境的循環)。 最后,顯示結果。 * * * ## `While`循環與`else` 與`for`循環相同,[`while`循環也可以具有可選的`else`塊](https://www.programiz.com/python-programming/for-loop)。 如果`while`循環中的條件求值為`False`,則執行`else`部分。 `while`循環可以使用[`break`語句](https://www.programiz.com/python-programming/break-continue)終止。 在這種情況下,`else`部分將被忽略。 因此,如果沒有中斷發生并且條件為假,則`while`循環的`else`部分將運行。 這是一個例子來說明這一點。 ```py '''Example to illustrate the use of else statement with the while loop''' counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print("Inside else") ``` **輸出** ```py Inside loop Inside loop Inside loop Inside else ``` 在這里,我們使用計數器變量將字符串內部循環打印三遍。 在第四次迭代中,`while`中的條件變為`False`。 因此,執行`else`部分。
                  <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>

                              哎呀哎呀视频在线观看