<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 功能強大 支持多語言、二開方便! 廣告
                # PythonWhile 循環語句 Python 編程中 while 語句用于循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。其基本形式為: ~~~ while 判斷條件(condition): 執行語句(statements)…… ~~~ 執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。 當判斷條件假 false 時,循環結束。 執行流程圖如下: ![](https://www.runoob.com/wp-content/uploads/2013/11/886A6E10-58F1-4A9B-8640-02DBEFF0EF9A.jpg) ### Gif 演示 Python while 語句執行過程 ![](https://www.runoob.com/wp-content/uploads/2014/05/006faQNTgw1f5wnm06h3ug30ci08cake.gif) 復雜一點: ![](https://www.runoob.com/wp-content/uploads/2013/11/loop-over-python-list-animation.gif) ``` #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" ``` 以上代碼執行輸出結果: ~~~ The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye! ~~~ while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用于跳過該次循環,break 則是用于退出循環,此外"判斷條件"還可以是個常值,表示循環必定成立,具體用法如下: ``` # continue 和 break 用法 i = 1 while i < 10: i += 1 if i%2 > 0: # 非雙數時跳過輸出 continue print i # 輸出雙數2、4、6、8、10 i = 1 while 1: # 循環條件為1必定成立 print i # 輸出1~10 i += 1 if i > 10: # 當i大于10時跳出循環 break ``` ## 無限循環 如果條件判斷語句永遠為 true,循環將會無限的執行下去,如下實例: ``` #!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 該條件永遠為true,循環將無限執行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!" ``` 以上實例輸出結果: ~~~ Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "test.py", line 5, in <module> num = raw_input("Enter a number :") KeyboardInterrupt ~~~ **意:**以上的無限循環你可以使用 CTRL+C 來中斷循環。 * * * ## 循環使用 else 語句 在 python 中,while … else 在循環條件為 false 時執行 else 語句塊: ``` #!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" ``` 以上實例輸出結果為: ~~~ 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 ~~~ ## 簡單語句組 類似 if 語句的語法,如果你的 while 循環體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示: ## 實例 #!/usr/bin/python ``` flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!" ``` **注意:**以上的無限循環你可以使用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>

                              哎呀哎呀视频在线观看