# 練習25. 更多更多的練習
我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是:寫程序,逐行研究,弄懂它。
過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。
~~~
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解析器中,以交互的方式和你寫的`ex25.py`文件交流,你可以像下面這樣在命令行中啟動python解析器:
~~~
$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
~~~
你的輸出應該和我類似,在`>`符號之后,你可以輸入并立即執行python代碼。我希望你用這種方式逐行輸入下方的python代碼,并看看他們有什么用:
~~~
import ex25
sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
words
sorted_words = ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words = ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)
~~~
這是我做出來的樣子:
~~~
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] 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.
>>> 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
~~~
當你寫完這些代碼,確保你能在`ex25.py`中找到運行的函數,并且明白它們每一個都是如何工作的。如果你的運行結果出錯或者跟我的結果不一樣,你就需要檢查并修復你的代碼,重啟python解析器,再次運行程序。
## 附加題
> 1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模塊`ex25`中定義的函數。
> 1. 試著執行`help(ex25)`和`help(ex25.break_words)`。這是你得到模塊幫助文檔的方式。 所謂幫助文檔就是你定義函數時放在`"""`之間的東西,它們也被稱作`documentation comments (文檔注解)`,后面你還會看到更多類似的東西。
> 1. 重復鍵入`ex25.` 是很煩的一件事情。有一個捷徑就是用`from ex25 import *`的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。
> 1. 把你腳本里的內容逐行通過python編譯器執行,看看會是什么樣子。你可以通過輸入`quit()`來退出Python。
## 常見問題
### Q: 我的某些函數沒有打印輸出任何值
> 你的函數末尾可能缺少`return`語句,檢查你的文件,確保每一行代碼的正確性。
### Q: 我輸入`import ex25`的時候遇到報錯`import: command not found`.
> 仔細觀察“你看到的結果”部分,看我是如何運行程序的。我是在python解析器里而不是在命令行運行程序。你應該先運行python解析器。
### Q: 當我輸入`import ex25.py`的時候,我遇到報錯`ImportError: No module named ex25.py`.
> 不要加上`.py`。Python知道文件是以`.py`結尾的,所以你只要輸入`import ex25`就可以了。
### Q:運行程序是,遇到報錯信息`SyntaxError: invalid syntax`
> 這個信息說明在報錯的這一行或之前的某一行你可能少寫了一個`(` 或者 `"` 或者其它的語法錯誤。當你遇到這個報錯的時候,從報錯的行開始,向上檢查是否每一行代碼都是正確的。
### Q:函數`words.pop`是如何改變變量`words`的值的?
> 這是一個復雜的問題,在這個實例中,`words`是一個列表,所以你可以調用它的一些命令,而它也會保留這些命令的結果。這類似于文件的工作原理。
### Q: 什么情況下,我可以在函數中用`print`代替`return`?
> `return`是從函數給出的代碼行調用的函數的結果。你可以把函數理解成 通過參數獲取輸入,并通過`return`返回輸出,而`print`是與這個過程完全無關的,它只負責在終端打印輸出。
- 序言
- 前言
- 簡介
- 0:安裝和準備
- 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:IF 語句
- 30:Else 和 If
- 31:做出決定
- 32:循環和列表
- 33:while循環
- 34:訪問列表元素
- 35:分支和函數
- 36:設計和調試
- 37:復習符號
- 38:列表操作
- 39:字典,可愛的字典
- 40:模塊, 類和對象
- 41:學會說面向對象
- 42:對象、類、以及從屬關系
- 43:基本的面向對象的分析和設計
- 44:繼承Vs.包含
- 45:你來制作一個游戲
- 46:項目骨架
- 47:自動化測試
- 48:更復雜的用戶輸入
- 49:寫代碼語句
- 50:你的第一個網站
- 51:從瀏覽器獲取輸入
- 52:開始你的web游戲
- 來自老程序員的建議
- 下一步
- 附錄A:命令行教程
- 簡介
- 安裝和準備
- 路徑, 文件夾, 名錄 (pwd)
- 如果你迷路了
- 創建一個路徑 (mkdir)
- 改變當前路徑 (cd)
- 列出當前路徑 (ls)
- 刪除路徑 (rmdir)
- 目錄切換(pushd, popd)
- 生成一個空文件(Touch, New-Item)
- 復制文件 (cp)
- 移動文件 (mv)
- 查看文件 (less, MORE)
- 輸出文件 (cat)
- 刪除文件 (rm)
- 退出命令行 (exit)
- 下一步