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

                    習題 21: 函數可以返回東西?

                    你已經學過使用 = 給變量命名,以及將變量定義為某個數字或者字符串。接下來我們將讓你見證更多奇跡。我們要演示給你的是如何使用 = 以及一個新的 Python 詞匯return 來將變量設置為“一個函數的值”。有一點你需要及其注意,不過我們暫且不講,先撰寫下面的腳本吧:

                     1
                     2
                     3
                     4
                     5
                     6
                     7
                     8
                     9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21
                    22
                    23
                    24
                    25
                    26
                    27
                    28
                    29
                    30
                    31
                    32
                    33
                    def add(a, b):
                        print "ADDING %d + %d" % (a, b)
                        return a + b
                    
                    def subtract(a, b):
                        print "SUBTRACTING %d - %d" % (a, b)
                        return a - b
                    
                    def multiply(a, b):
                        print "MULTIPLYING %d * %d" % (a, b)
                        return a * b
                    
                    def divide(a, b):
                        print "DIVIDING %d / %d" % (a, b)
                        return a / b
                    
                    
                    print "Let's do some math with just functions!"
                    
                    age = add(30, 5)
                    height = subtract(78, 4)
                    weight = multiply(90, 2)
                    iq = divide(100, 2)
                    
                    print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
                    
                    
                    # A puzzle for the extra credit, type it in anyway.
                    print "Here is a puzzle."
                    
                    what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
                    
                    print "That becomes: ", what, "Can you do it by hand?"
                    

                    現在我們創建了我們自己的加減乘除數學函數: add, subtract, multiply, 以及 divide。重要的是函數的最后一行,例如 add 的最后一行是 return a + b,它實現的功能是這樣的:

                    1. 我們調用函數時使用了兩個參數: ab
                    2. 我們打印出這個函數的功能,這里就是計算加法(adding)
                    3. 接下來我們告訴 Python 讓它做某個回傳的動作:我們將 a + b 的值返回(return)。或者你可以這么說:“我將 ab 加起來,再把結果返回。”
                    4. Python 將兩個數字相加,然后當函數結束的時候,它就可以將 a + b 的結果賦予一個變量。

                    和本書里的很多其他東西一樣,你要慢慢消化這些內容,一步一步執行下去,追蹤一下究竟發生了什么。為了幫助你理解,本節的加分習題將讓你解決一個迷題,并且讓你學到點比較酷的東西。

                    你應該看到的結果?

                    $ python ex21.py
                    Let's do some math with just functions!
                    ADDING 30 + 5
                    SUBTRACTING 78 - 4
                    MULTIPLYING 90 * 2
                    DIVIDING 100 / 2
                    Age: 35, Height: 74, Weight: 180, IQ: 50
                    Here is a puzzle.
                    DIVIDING 50 / 2
                    MULTIPLYING 180 * 25
                    SUBTRACTING 74 - 4500
                    ADDING 35 + -4426
                    That becomes:  -4391 Can you do it by hand?
                    $
                    

                    加分習題?

                    1. 如果你不是很確定 return 的功能,試著自己寫幾個函數出來,讓它們返回一些值。你可以將任何可以放在 = 右邊的東西作為一個函數的返回值。
                    2. 這個腳本的結尾是一個迷題。我將一個函數的返回值用作了另外一個函數的參數。我將它們鏈接到了一起,就跟寫數學等式一樣。這樣可能有些難讀,不過運行一下你就知道結果了。接下來,你需要試試看能不能用正常的方法實現和這個表達式一樣的功能。
                    3. 一旦你解決了這個迷題,試著修改一下函數里的某些部分,然后看會有什么樣的結果。你可以有目的地修改它,讓它輸出另外一個值。
                    4. 最后,顛倒過來做一次。寫一個簡單的等式,使用一樣的函數來計算它。

                    這個習題可能會讓你有些頭大,不過還是慢慢來,把它當做一個游戲,解決這樣的迷題正是編程的樂趣之一。后面你還會看到類似的小謎題。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 20: 函數和文件

                    Next topic

                    習題 22: 到現在你學到了哪些東西?

                    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>

                                      这里只有精品视频