<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國際加速解決方案。 廣告
                # 習題 30: Else 和 If 前一習題中你寫了一些 “if 語句(if-statements)”,并且試圖猜出它們是什么,以及實現的是什么功能。在你繼續學習之前,我給你解釋一下上一節的加分習題的答案。上一節的加分習題你做過了吧,有沒有? 1. 你認為 if 對于它下一行的代碼做了什么? If 語句為代碼創建了一個所謂的“分支”,就跟 RPG 游戲中的情節分支一樣。if 語句告訴你的腳本:“如果這個布爾表達式為真,就運行接下來的代碼,否則就跳過這一段。” 1. 為什么 if 語句的下一行需要 4 個空格的縮進? 行尾的冒號的作用是告訴 Python 接下來你要創建一個新的代碼區段。這根你創建函數時的冒號是一個道理。 1. 如果不縮進, 會發生什么事情? 如果你沒有縮進,你應該會看到 Python 報錯。Python 的規則里,只要一行以“冒號(colon)” : 結尾,它接下來的內容就應該有縮進。 1. 把習題 27 中的其它布爾表達式放到 if語句 中會不會也可以運行呢?試一下。 可以。而且不管多復雜都可以,雖然寫復雜的東西通常是一種不好的編程風格。 1. 如果把變量 people, cats, 和 dogs 的初始值改掉, 會發生什么事情? 因為你比較的對象是數字,如果你把這些數字改掉的話,某些位置的 if 語句會被演繹為 True,而它下面的代碼區段將被運行。你可以試著修改這些數字,然后在頭腦里假想一下那一段代碼會被運行。 把我的答案和你的答案比較一下,確認自己真正懂得代碼“區段”的含義。這點對于你下一節的練習很重要,因為你將會寫很多的 if 語句。 把這一段寫下來,并讓它運行起來: <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&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>people = 30&#13; cars = 40&#13; buses = 15&#13; &#13; &#13; if cars &gt; people:&#13; print "We should take the cars."&#13; elif cars &lt; people:&#13; print "We should not take the cars."&#13; else:&#13; print "We can't decide."&#13; &#13; if buses &gt; cars:&#13; print "That's too many buses."&#13; elif buses &lt; cars:&#13; print "Maybe we could take the buses."&#13; else:&#13; print "We still can't decide."&#13; &#13; if people &gt; buses:&#13; print "Alright, let's just take the buses."&#13; else:&#13; print "Fine, let's stay home then."&#13; </pre> </div> </td> </tr></tbody></table> ### 你應該看到的結果 ~~~ $ python ex30.py We should take the cars. Maybe we could take the buses. Alright, let's just take the buses. $ ~~~ ### 加分習題 1. 猜想一下 elif 和 else 的功能。 1. 將 cars, people, 和 buses 的數量改掉,然后追溯每一個 if 語句。看看最后會打印出什么來。 1. 試著寫一些復雜的布爾表達式,例如 cars>peopleandbuses<cars。 1. 在每一行的上面寫注解,說明這一行的功用。
                  <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>

                              哎呀哎呀视频在线观看