鍍金池/ 教程/ Python/ exercise25. 更多更多的練習(xí)
附錄 A-練習(xí) 9:生成一個(gè)空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動(dòng)文件 (mv)
exercise46.項(xiàng)目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號(hào)
exercise47.自動(dòng)化測(cè)試
exercise3.數(shù)字和數(shù)學(xué)計(jì)算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對(duì)象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡(jiǎn)介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫(xiě)代碼語(yǔ)句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對(duì)象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達(dá)式
附錄 A-練習(xí) 4:創(chuàng)建一個(gè)路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號(hào)“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個(gè)程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會(huì)說(shuō)面向?qū)ο?/span>
exercise52.開(kāi)始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來(lái)制作一個(gè)游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進(jìn)行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語(yǔ)句
exercise36.設(shè)計(jì)和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個(gè)網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫(xiě)文件
exercise4.變量和命名
exercise34.訪問(wèn)列表元素
exercise11.提問(wèn)
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計(jì)
附錄 A-簡(jiǎn)介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來(lái)自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛(ài)的字典

exercise25. 更多更多的練習(xí)

我們將做一些關(guān)于函數(shù)和變量的練習(xí),以確認(rèn)你真正掌握了這些知識(shí)。這節(jié)練習(xí)對(duì)你來(lái)說(shuō)可以說(shuō)是:寫(xiě)程序,逐行研究,弄懂它。

過(guò)這節(jié)練習(xí)還是有些不同,你不需要運(yùn)行它,取而代之,你需要將它導(dǎo)入到 python 里通過(guò)自己執(zhí)行函數(shù)的方式運(yùn)行。

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 運(yùn)行,找出里邊的錯(cuò)誤,并修正。然后你需要跟著下面的答案部分完成這節(jié)練習(xí)。

你看到的結(jié)果

在這節(jié)練習(xí)中,我們將在 Python 解析器中,以交互的方式和你寫(xiě)的 ex25.py 文件交流,你可以像下面這樣在命令行中啟動(dòng) 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.
>>>

你的輸出應(yīng)該和我類似,在>符號(hào)之后,你可以輸入并立即執(zhí)行 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)

這是我做出來(lái)的樣子:

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

當(dāng)你寫(xiě)完這些代碼,確保你能在 ex25.py 中找到運(yùn)行的函數(shù),并且明白它們每一個(gè)都是如何工作的。如果你的運(yùn)行結(jié)果出錯(cuò)或者跟我的結(jié)果不一樣,你就需要檢查并修復(fù)你的代碼,重啟 python 解析器,再次運(yùn)行程序。

附加題

1.研究答案中沒(méi)有分析過(guò)的行,找出它們的來(lái)龍去脈。確認(rèn)自己明白了自己使用的是模塊 ex25 中定義的函數(shù)。 2.試著執(zhí)行 help(ex25)和 help(ex25.break_words)。這是你得到模塊幫助文檔的方式。 所謂幫助文檔就是你定義函數(shù)時(shí)放在"""之間的東西,它們也被稱作 documentation comments (文檔注解),后面你還會(huì)看到更多類似的東西。 3.重復(fù)鍵入 ex25. 是很煩的一件事情。有一個(gè)捷徑就是用 from ex25 import *的方式導(dǎo)入模組。這相當(dāng)于說(shuō):“我要把 ex25 中所有的東西 import 進(jìn)來(lái)。”程序員喜歡說(shuō)這樣的倒裝句,開(kāi)一個(gè)新的會(huì)話,看看你所有的函數(shù)是不是已經(jīng)在那里了。 4.把你腳本里的內(nèi)容逐行通過(guò) python 編譯器執(zhí)行,看看會(huì)是什么樣子。你可以通過(guò)輸入 quit()來(lái)退出 Python。

常見(jiàn)問(wèn)題

Q: 我的某些函數(shù)沒(méi)有打印輸出任何值

你的函數(shù)末尾可能缺少 return 語(yǔ)句,檢查你的文件,確保每一行代碼的正確性。

Q: 我輸入 import ex25 的時(shí)候遇到報(bào)錯(cuò) import: command not found.

仔細(xì)觀察“你看到的結(jié)果”部分,看我是如何運(yùn)行程序的。我是在 python 解析器里而不是在命令行運(yùn)行程序。你應(yīng)該先運(yùn)行 python 解析器。

Q: 當(dāng)我輸入 import ex25.py 的時(shí)候,我遇到報(bào)錯(cuò) ImportError: No module named ex25.py.

不要加上.py。Python 知道文件是以.py 結(jié)尾的,所以你只要輸入 import ex25 就可以了。

Q:運(yùn)行程序是,遇到報(bào)錯(cuò)信息 SyntaxError: invalid syntax

這個(gè)信息說(shuō)明在報(bào)錯(cuò)的這一行或之前的某一行你可能少寫(xiě)了一個(gè)( 或者 " 或者其它的語(yǔ)法錯(cuò)誤。當(dāng)你遇到這個(gè)報(bào)錯(cuò)的時(shí)候,從報(bào)錯(cuò)的行開(kāi)始,向上檢查是否每一行代碼都是正確的。

Q:函數(shù) words.pop 是如何改變變量 words 的值的?

這是一個(gè)復(fù)雜的問(wèn)題,在這個(gè)實(shí)例中,words 是一個(gè)列表,所以你可以調(diào)用它的一些命令,而它也會(huì)保留這些命令的結(jié)果。這類似于文件的工作原理。

Q: 什么情況下,我可以在函數(shù)中用 print 代替 return?

return 是從函數(shù)給出的代碼行調(diào)用的函數(shù)的結(jié)果。你可以把函數(shù)理解成 通過(guò)參數(shù)獲取輸入,并通過(guò) return 返回輸出,而 print 是與這個(gè)過(guò)程完全無(wú)關(guān)的,它只負(fù)責(zé)在終端打印輸出。