<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國際加速解決方案。 廣告
                # while語句 只要在一個條件為真的情況下,`while`語句允許你重復執行一塊語句。`while`語句是所謂 循環 語句的一個例子。`while`語句有一個可選的`else`從句。 ``` #!/usr/bin/python # Filename: while.py number = 23 running = True while running: ????guess = int(raw_input('Enter an integer : ')) ????if guess == number: ????????print 'Congratulations, you guessed it.' ????????running = False # this causes the while loop to stop ????elif guess &lt; number: ????????print 'No, it is a little higher than that' ????else: ????????print 'No, it is a little lower than that' else: ????print 'The while loop is over.' ????# Do anything else you want to do here print 'Done' ``` (源文件:[code/while.py](code/while.py)) ## 輸出 ``` $ python while.py Enter an integer : 50 No, it is a little lower than that. Enter an integer : 22 No, it is a little higher than that. Enter an integer : 23 Congratulations, you guessed it. The while loop is over. Done ``` ## 它如何工作 在這個程序中,我們仍然使用了猜數游戲作為例子,但是這個例子的優勢在于用戶可以不斷的猜數,直到他猜對為止——這樣就不需要像前面那個例子那樣為每次猜測重復執行一遍程序。這個例子恰當地說明了`while`語句的使用。 我們把`raw_input`和`if`語句移到了`while`循環內,并且在while循環開始前把`running`變量設置為`True`。首先,我們檢驗變量`running`是否為`True`,然后執行后面的 while-塊 。在執行了這塊程序之后,再次檢驗條件,在這個例子中,條件是`running`變量。如果它是真的,我們再次執行while-塊,否則,我們繼續執行可選的else-塊,并接著執行下一個語句。 當`while`循環條件變為`False`的時候,`else`塊才被執行——這甚至也可能是在條件第一次被檢驗的時候。如果`while`循環有一個`else`從句,它將始終被執行,除非你的`while`循環將永遠循環下去不會結束! `True`和`False`被稱為布爾類型。你可以分別把它們等效地理解為值`1`和`0`。在檢驗重要條件的時候,布爾類型十分重要,它們并不是真實的值`1`。 else塊事實上是多余的,因為你可以把其中的語句放在同一塊(與`while`相同)中,跟在`while`語句之后,這樣可以取得相同的效果。 給C/C++程序員的注釋 記住,你可以在`while`循環中使用一個`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>

                              哎呀哎呀视频在线观看