# 習題 25: 更多更多的練習
我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是一本道:寫程序,逐行研究,弄懂它。
不過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。
<table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>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)
</pre> </div> </td> </tr></tbody></table>
首先以正常的方式 pythonex25.py 運行,找出里邊的錯誤,并把它們都改正過來。然后你需要接著下面的答案章節完成這節練習。
### 你應該看到的結果
這節練習我們將在你之前用來做算術的 python 編譯器里,用交互的方式和你的.py 作交流。
這是我做出來的樣子:
<table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>$ 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
$
</pre> </div> </td> </tr></tbody></table>
我們來逐行分析一下每一步實現的是什么:
- 在第 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_word 和 ex25.print_last_word 將第一個和最后一個詞打印出來。
- 第 17 行比較有趣。我把 words 變量寫錯成了 wrods,所以 python 在 18-20 行給了一個錯誤信息。
- 21-22 行我們打印出了修改過的詞匯列表。第一個和最后一個單詞我們已經打印過了,所以在這里沒有再次打印出來。
剩下的行你需要自己分析一下,就留作你的加分習題了。
### 加分習題
1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 ex25 中定義的函數。
1. 試著執行 help(ex25) 和 help(ex25.break_words) 。這是你得到模組幫助文檔的方式。 所謂幫助文檔就是你定義函數時放在 """ 之間的東西,它們也被稱作 documentationcomments (文檔注解),后面你還會看到更多類似的東西。
1. 重復鍵入 ex25. 是很煩的一件事情。有一個捷徑就是用 fromex25import* 的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。
1. 把你腳本里的內容逐行通過 python 編譯器執行,看看會是什么樣子。你可以執行CTRL-D (Windows 下是 CTRL-Z)來關閉編譯器。
- 譯者前言
- 前言:笨辦法更簡單
- 習題 0: 準備工作
- 習題 1: 第一個程序
- 習題 2: 注釋和井號
- 習題 3: 數字和數學計算
- 習題 4: 變量(variable)和命名
- 習題 5: 更多的變量和打印
- 習題 6: 字符串(string)和文本
- 習題 7: 更多打印
- 習題 8: 打印,打印
- 習題 9: 打印,打印,打印
- 習題 10: 那是什么?
- 習題 11: 提問
- 習題 12: 提示別人
- 習題 13: 參數、解包、變量
- 習題 14: 提示和傳遞
- 習題 15: 讀取文件
- 習題 16: 讀寫文件
- 習題 17: 更多文件操作
- 習題 18: 命名、變量、代碼、函數
- 習題 19: 函數和變量
- 習題 20: 函數和文件
- 習題 21: 函數可以返回東西
- 習題 22: 到現在你學到了哪些東西?
- 習題 23: 讀代碼
- 習題 24: 更多練習
- 習題 25: 更多更多的練習
- 習題 26: 恭喜你,現在可以考試了!
- 習題 27: 記住邏輯關系
- 習題 28: 布爾表達式練習
- 習題 29: 如果(if)
- 習題 30: Else 和 If
- 習題 31: 作出決定
- 習題 32: 循環和列表
- 習題 33: While 循環
- 習題 34: 訪問列表的元素
- 習題 35: 分支和函數
- 習題 36: 設計和調試
- 習題 37: 復習各種符號
- 習題 38: 閱讀代碼
- 習題 39: 列表的操作
- 習題 40: 字典, 可愛的字典
- 習題 41: 來自 Percal 25 號行星的哥頓人(Gothons)
- 習題 42: 物以類聚
- 習題 43: 你來制作一個游戲
- 習題 44: 給你的游戲打分
- 習題 45: 對象、類、以及從屬關系
- 習題 46: 一個項目骨架
- 習題 47: 自動化測試
- 習題 48: 更復雜的用戶輸入
- 習題 49: 創建句子
- 習題 50: 你的第一個網站
- 習題 51: 從瀏覽器中獲取輸入
- 習題 52: 創建你的 web 游戲
- 下一步
- 老程序員的建議