在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

exercise25. 更多更多的練習

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

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

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

你看到的結果

在這節(jié)練習中,我們將在 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.
>>>

你的輸出應該和我類似,在>符號之后,你可以輸入并立即執(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)

這是我做出來的樣子:

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 中找到運行的函數(shù),并且明白它們每一個都是如何工作的。如果你的運行結果出錯或者跟我的結果不一樣,你就需要檢查并修復你的代碼,重啟 python 解析器,再次運行程序。

附加題

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

常見問題

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

你的函數(shù)末尾可能缺少 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:函數(shù) words.pop 是如何改變變量 words 的值的?

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

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

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