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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 4.2.3 用函數增強程序的通用性 我們說過,程序 4.4 在減少重復代碼和模塊化兩方面已經做得很好,但這并不意味著該 程序在各方面都已經完美。例如,如果我們希望換用字符"^"再畫一棵樹,以便比較哪個更 好看些,該如何做呢?顯見的做法是仿照用"*"畫樹的代碼重寫畫樹冠的函數,而樹干部分 可以重用。于是得到下面的代碼: 【程序 4.5】tree5.py ``` def treetop1(): print " *" print " ***" print " *****" print "*******" def treetop2(): print " ^" print " ^^^" print " ^^^^^" print "^^^^^^^" def star_treetop(): treetop1() treetop1() def caret_treetop(): treetop2() treetop2() def treetrunk(): print " #" print " #" print " #" def main(): star_treetop() treetrunk() print caret_treetop() treetrunk() main() ``` 此版本的執行結果如下: ``` * *** ***** ******* * *** ***** ******* # # # ^ ^^^ ^^^^^ ^^^^^^^ ^ ^^^ ^^^^^ ^^^^^^^ # # # ``` 雖然程序 4.5 滿足了功能需求,但是從程序設計角度說是很笨拙的,因為這是一種“頭 痛醫頭腳痛醫腳”的方法,即為每一種特殊情形創建新的代碼。更好的做法是用一個一般的 函數來處理所有特殊情形。鑒于 treetop1 和 treetop2 的非常類似,我們可以從他們抽 象出一個通用的畫樹冠的函數,使得該函數能夠取代 treetop1 和 treetop2。 函數的通用性可以通過引入參數(parameter)來實現。要理解參數的作用,可以簡單地與數學函數的自變量進行類比。以函數 f(x) = x2 為例,對于給定的自變量值 10,函數計算 出函數值 f(10) = 100;換不同的自變量值 20,則函數又計算出另一個函數值 f(20) = 400。編 程語言中的函數參數具有類似的行為,輸入不同的參數值,則函數執行后可產生不同的結果。 下面我們設計一個通用的畫樹冠的函數 treetop(ch),其中參數 ch 表示用來作畫的 字符。為了控制樹的形狀,函數定義中使用了字符串格式化運算。 ``` >>> def treetop(ch): print " %s" % (ch) print " %s" % (3 * ch) print " %s" % (5 * ch) print "%s" % (7 * ch) ``` 在交互環境定義了函數 treetop(ch)后,我們接著來測試它的效果。下面是測試例子: ``` >>> treetop('*') * *** ***** ******* >>> treetop('^') ^ ^^^ ^^^^^ ^^^^^^^ >>> treetop('A') A AAA AAAAA AAAAAAA ``` 可見函數 treetop(ch)確實具有通用性,只要為它的參數提供一個字符值,就能用該字符 畫出樹冠形狀。下面我們利用 treetop(ch)函數來改寫程序 4.5: 【程序 4.6】tree6.py ``` def treetop(ch): print " %s" % (ch) print " %s" % (3 * ch) print " %s" % (5 * ch) print "%s" % (7 * ch) def star_treetop(): treetop("*") treetop("*") def caret_treetop(): treetop("^") treetop("^") def treetrunk(): print " #" print " #" print " #" def main(): star_treetop() treetrunk() print caret_treetop() treetrunk() main() ``` 此版本的執行結果和程序 4.5 完全一樣,但是比較兩者的代碼會發現,程序 4.6 將程序 4.5 中的兩個函數合二為一,增強了通用性。以后如果想換用其他字符畫樹,修改程序 4.6 比修 改程序 4.5 要簡單得多。
                  <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>

                              哎呀哎呀视频在线观看