<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    習題 30: Else 和 If?

                    前一習題中你寫了一些 “if 語句(if-statements)”,并且試圖猜出它們是什么,以及實現的是什么功能。在你繼續學習之前,我給你解釋一下上一節的加分習題的答案。上一節的加分習題你做過了吧,有沒有?

                    1. 你認為 if 對于它下一行的代碼做了什么? If 語句為代碼創建了一個所謂的“分支”,就跟 RPG 游戲中的情節分支一樣。if 語句告訴你的腳本:“如果這個布爾表達式為真,就運行接下來的代碼,否則就跳過這一段。”
                    2. 為什么 if 語句的下一行需要 4 個空格的縮進? 行尾的冒號的作用是告訴 Python 接下來你要創建一個新的代碼區段。這根你創建函數時的冒號是一個道理。
                    3. 如果不縮進, 會發生什么事情? 如果你沒有縮進,你應該會看到 Python 報錯。Python 的規則里,只要一行以“冒號(colon)” : 結尾,它接下來的內容就應該有縮進。
                    4. 把習題 27 中的其它布爾表達式放到 if語句 中會不會也可以運行呢?試一下。 可以。而且不管多復雜都可以,雖然寫復雜的東西通常是一種不好的編程風格。
                    5. 如果把變量 people, cats, 和 dogs 的初始值改掉, 會發生什么事情? 因為你比較的對象是數字,如果你把這些數字改掉的話,某些位置的 if 語句會被演繹為 True,而它下面的代碼區段將被運行。你可以試著修改這些數字,然后在頭腦里假想一下那一段代碼會被運行。

                    把我的答案和你的答案比較一下,確認自己真正懂得代碼“區段”的含義。這點對于你下一節的練習很重要,因為你將會寫很多的 if 語句。

                    把這一段寫下來,并讓它運行起來:

                     1
                     2
                     3
                     4
                     5
                     6
                     7
                     8
                     9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21
                    22
                    23
                    people = 30
                    cars = 40
                    buses = 15
                    
                    
                    if cars > people:
                        print "We should take the cars."
                    elif cars < people:
                        print "We should not take the cars."
                    else:
                        print "We can't decide."
                    
                    if buses > cars:
                        print "That's too many buses."
                    elif buses < cars:
                        print "Maybe we could take the buses."
                    else:
                        print "We still can't decide."
                    
                    if people > buses:
                        print "Alright, let's just take the buses."
                    else:
                        print "Fine, let's stay home then."
                    

                    你應該看到的結果?

                    $ python ex30.py
                    We should take the cars.
                    Maybe we could take the buses.
                    Alright, let's just take the buses.
                    $
                    

                    加分習題?

                    1. 猜想一下 elifelse 的功能。
                    2. cars, people, 和 buses 的數量改掉,然后追溯每一個 if 語句。看看最后會打印出什么來。
                    3. 試著寫一些復雜的布爾表達式,例如 cars > people and buses < cars
                    4. 在每一行的上面寫注解,說明這一行的功用。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 29: 如果(if)

                    Next topic

                    習題 31: 作出決定

                    This Page

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频