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

                    習題 25: 更多更多的練習?

                    我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是一本道:寫程序,逐行研究,弄懂它。

                    不過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。

                     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
                    34
                    35
                    def break_words(stuff):
                        """This function will break up words for us."""
                        words = stuff.split(' ')
                        return words
                    
                    def sort_words(words):
                        """Sorts the words."""
                        return sorted(words)
                    
                    def print_first_word(words):
                        """Prints the first word after popping it off."""
                        word = words.pop(0)
                        print word
                    
                    def print_last_word(words):
                        """Prints the last word after popping it off."""
                        word = words.pop(-1)
                        print word
                    
                    def sort_sentence(sentence):
                        """Takes in a full sentence and returns the sorted words."""
                        words = break_words(sentence)
                        return sort_words(words)
                    
                    def print_first_and_last(sentence):
                        """Prints the first and last words of the sentence."""
                        words = break_words(sentence)
                        print_first_word(words)
                        print_last_word(words)
                    
                    def print_first_and_last_sorted(sentence):
                        """Sorts the words then prints the first and last one."""
                        words = sort_sentence(sentence)
                        print_first_word(words)
                        print_last_word(words)
                    

                    首先以正常的方式 python ex25.py 運行,找出里邊的錯誤,并把它們都改正過來。然后你需要接著下面的答案章節完成這節練習。

                    你應該看到的結果?

                    這節練習我們將在你之前用來做算術的 python 編譯器里,用交互的方式和你的.py 作交流。

                    這是我做出來的樣子:

                     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
                    34
                    35
                    36
                    37
                    38
                    39
                    $ python
                    Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
                    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
                    Type "help", "copyright", "credits" or "license" for more information.
                    >>> import ex25
                    >>> sentence = "All good things come to those who wait."
                    >>> words = ex25.break_words(sentence)
                    >>> words
                    ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
                    >>> sorted_words = ex25.sort_words(words)
                    >>> sorted_words
                    ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
                    >>> ex25.print_first_word(words)
                    All
                    >>> ex25.print_last_word(words)
                    wait.
                    >>> wrods
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    NameError: name 'wrods' is not defined
                    >>> words
                    ['good', 'things', 'come', 'to', 'those', 'who']
                    >>> ex25.print_first_word(sorted_words)
                    All
                    >>> ex25.print_last_word(sorted_words)
                    who
                    >>> sorted_words
                    ['come', 'good', 'things', 'those', 'to', 'wait.']
                    >>> sorted_words = ex25.sort_sentence(sentence)
                    >>> sorted_words
                    ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
                    >>> ex25.print_first_and_last(sentence)
                    All
                    wait.
                    >>> ex25.print_first_and_last_sorted(sentence)
                    All
                    who
                    >>> ^D
                    $
                    

                    我們來逐行分析一下每一步實現的是什么:

                    • 在第 5 行你將自己的 ex25.py 執行了 import,和你做過的其它 import 一樣。在 import 的時候你不需要加 .py 后綴。這個過程里,你把 ex25.py 當做了一個“模組(module)”來使用,你在這個模組里定義的函數也可以直接調用出來。
                    • 第 6 行你創建了一個用來處理的“句子(sentence)”。
                    • 第 7 行你使用 ex25 調用你的第一個函數 ex25.break_words。其中的 . (dot, period)符號可以告訴 Python:“嗨,我要運行 ex25 里的哪個個叫 break_words 的函數!”
                    • 第 8 行我們只是輸入 words,而 python 將在第 9 行打印出這個變量里邊有什么。看上去可能會覺得奇怪,不過這其實是一個“列表(list)”,你會在后面的章節中學到它。
                    • 10-11 行我們使用 ex25.sort_words 來得到一個排序過的句子。
                    • 13-16 行我們使用 ex25.print_first_wordex25.print_last_word 將第一個和最后一個詞打印出來。
                    • 第 17 行比較有趣。我把 words 變量寫錯成了 wrods,所以 python 在 18-20 行給了一個錯誤信息。
                    • 21-22 行我們打印出了修改過的詞匯列表。第一個和最后一個單詞我們已經打印過了,所以在這里沒有再次打印出來。

                    剩下的行你需要自己分析一下,就留作你的加分習題了。

                    加分習題?

                    1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 ex25 中定義的函數。
                    2. 試著執行 help(ex25)help(ex25.break_words) 。這是你得到模組幫助文檔的方式。 所謂幫助文檔就是你定義函數時放在 """ 之間的東西,它們也被稱作 documentation comments (文檔注解),后面你還會看到更多類似的東西。
                    3. 重復鍵入 ex25. 是很煩的一件事情。有一個捷徑就是用 from ex25 import * 的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。
                    4. 把你腳本里的內容逐行通過 python 編譯器執行,看看會是什么樣子。你可以執行CTRL-D (Windows 下是 CTRL-Z)來關閉編譯器。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 24: 更多練習

                    Next topic

                    習題 26: 恭喜你,現在可以考試了!

                    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>

                                      这里只有精品视频