練習(xí) 25 更更多練習(xí)
接下來我們要做更多包含函數(shù)和變量的練習(xí),來確保你完全掌握這些東西朴恳。這個(gè)練習(xí)你應(yīng)該能直接輸入抄罕、拆解并理解。
不過于颖,這個(gè)練習(xí)有一些不同呆贿,你不是運(yùn)行它,而是要把它導(dǎo)入 Python 然后自己運(yùn)行這個(gè)函數(shù)。
ex25.py
1 def break_words(stuff):
2 """This function will break up words for us."""
3 words = stuff.split(' ')
4 return words
5
6 def sort_words(words):
7 """Sorts the words."""
8 return sorted(words)
9
10 def print_first_word(words):
11 """Prints the first word after popping it off."""
12 word = words.pop(0)
13 print(word)
14
15 def print_last_word(words):
16 """Prints the last word after popping it off."""
17 word = words.pop(-1)
18 print(word)
19
20 def sort_sentence(sentence):
21 """Takes in a full sentence and returns the sorted words
22 words = break_words(sentence)
23 return sort_words(words)
24
25 def print_first_and_last(sentence):
26 """Prints the first and last words of the sentence."""
27 words = break_words(sentence)
28 print_first_word(words)
29 print_last_word(words)
30
31 def print_first_and_last_sorted(sentence):
32 """Sorts the words then prints the first and last one.""
33 words = sort_sentence(sentence)
34 print_first_word(words)
35 print_last_word(words)
首先做入,用 python3.6 ex25.py
來運(yùn)行這個(gè)腳本冒晰,找出你出錯(cuò)的地方,并把它們改正過來竟块。然后對(duì)照“你會(huì)看到”部分看看運(yùn)行結(jié)果是否一樣壶运。
你會(huì)看到
在這個(gè)練習(xí)中我們要在 python3.6 翻譯器(interpreter)里與 ex25.py
文件交互,之前我們?cè)谧鲇?jì)算的時(shí)候也交互過浪秘。你在終端里這樣運(yùn)行 python3.6(Windows 下直接輸入 python
):
$ python3.6
Python 3.6.0rc2 (v3.6.0rc2:800a67f7806d, Dec 16 2016, 14:12:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help " , "copyright" , "credits" or "license" for more info
>>>
你的輸出結(jié)果應(yīng)該和我的一樣蒋情,你可以在提示符(即 >
)后面輸入 Python 代碼,它會(huì)直接運(yùn)行耸携。我希望你用這種方式輸入這個(gè)練習(xí)的每一行代碼棵癣,然后看看會(huì)如何:
練習(xí) 25 會(huì)話
1 import ex25
2 sentence = "All good things come to those who wait."
3 words = ex25.break_words(sentence)
4 words
5 sorted_words = ex25.sort_words(words)
6 sorted_words
7 ex25.print_first_word(words)
8 ex25.print_last_word(words)
9 words
10 ex25.print_first_word(sorted_words)
11 ex25.print_last_word(sorted_words)
12 sorted_words
13 sorted_words = ex25.sort_sentence(sentence)
14 sorted_words
15 ex25.print_first_and_last(sentence)
16 ex25.print_first_and_last_sorted(sentence)
以下是交互模式下輸入的結(jié)果:
練習(xí) 25 Python 會(huì)話
Python 3.6.0 (default, Feb 2 2017, 12:48:29)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwi
Type "help", "copyright", "credits" or "license" for more informa
>>> 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
當(dāng)你過完每一行,保證你能找到在 ex25.py
中運(yùn)行的函數(shù)夺衍,并且理解了每個(gè)函數(shù)是如何運(yùn)行的狈谊。如果你得到了不同的結(jié)果或者出現(xiàn)錯(cuò)誤,你得把代碼改正過來刷后,然后退出 python3.6
的畴,重新進(jìn)入。
附加練習(xí)
- 弄明白“你會(huì)看到”中各行的作用是什么尝胆,確保你理解你是如何在 ex25 模塊中運(yùn)行你的函數(shù)的丧裁。
- 試試輸入
help(ex25)
以及help(ex25.break_words)
(要在交互練習(xí)后輸入,否則無法成功運(yùn)行)含衔。注意你是如何獲取到關(guān)于這個(gè)模塊的幫助的煎娇,以及幫助是如何放在 ex25 的每一個(gè)函數(shù)后面的"""
字符串里的。 這些特殊的字符串被稱為文件注釋贪染,我們會(huì)在后面看到更多缓呛。- 輸入
ex25.
很無聊,可以走個(gè)捷徑:from ex25 import *
杭隙,意思就是從 ex25 導(dǎo)入所有東西哟绊,程序員總喜歡倒著說。打開一個(gè)新會(huì)話痰憎,看看你的函數(shù)會(huì)如何票髓。- 試著拆解你的文件,看看當(dāng)你用它的時(shí)候铣耘,它在 Python 里是什么樣的洽沟。你得先輸入
quit()
來退出 python,再重新加載它蜗细。
常見問題
有些函數(shù)我什么都沒打印出來裆操。你可能有些函數(shù)忘了在后面輸入 return
。檢查一遍你的代碼,確保每一行都是對(duì)的踪区。
當(dāng)我輸入 import ex25
之后昆烁,我收到了 -bash: import: command not found.
注意看“你會(huì)看到”部分我是怎么做的。我是在 Python 里面運(yùn)行的朽缴,而不是在 Terminal善玫,也就是說,你得先運(yùn)行 Python密强。
當(dāng)我輸入 import ex25.py
時(shí)收到了這樣的錯(cuò)誤:ImportError: No module named ex25
茅郎。 不要在后面加 .py
,Python 知道文件是以 .py
結(jié)尾的或渤,所以你只用輸入 ex25
即可系冗。
我運(yùn)行的時(shí)候遇到了這個(gè)錯(cuò)誤:SyntaxError: invalid syntax
。這意味著你漏掉了某些東西薪鹦,比如少了一個(gè) " 或者類似一對(duì)的符號(hào)掌敬。任何時(shí)候你只要收到這樣的報(bào)錯(cuò)信息,你就從它提到的錯(cuò)的那行開始檢查池磁,看是不是所有字符都輸入正確了奔害,然后再回過頭檢查這一行上面的行是不是都輸入正確了。
words.pop
函數(shù)是如何改變 words
變量的地熄?這是個(gè)很復(fù)雜的問題华临,但是在本例中 words
是一個(gè)列表,正因?yàn)槿绱四憧梢越o它一些命令端考。這就類似于當(dāng)你操作文件和很多其他東西時(shí)候它們是如何運(yùn)行的一樣雅潭。
在函數(shù)里我什么時(shí)候應(yīng)該用 print
而不是 return
呢?通過函數(shù)却特,return
能夠給調(diào)用這個(gè)函數(shù)的那行代碼返回一個(gè)結(jié)果扶供,你可以把函數(shù)當(dāng)成通過參數(shù)獲取輸入通過 return
返回輸出。print
跟這個(gè)就完全不相關(guān)了裂明,它只是把輸出結(jié)果打印到終端椿浓。