《笨办法学python》6_笨办法学Python 习题 25: 更多更多的练习
我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是一本道:寫程序,逐行研究,弄懂它。
不過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。
# -*- coding:utf-8 -*-
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
#將指定字符串進行分割 以空格為標志分割字符串,默認全部分割,可以在括號里”后面指定參數以使解釋器按規定次數分割。
return words
def sort_words(words):
#以字母表順序為依據將words變量所包含的字符串中的英文單詞進行排序,英文句號在該過程中將被舍棄。
"""Sorts the words."""
return sorted(words)
#sorted 排序
def print_first_word(words):
"""Prints the first word afteer popping it off."""
word = words.pop(0)
#彈出一個元素后關閉,括號內的參數表示彈出元素的位置。0代表第一個,-1代表最后一個。
# 暫不清楚單位是不是之前類似的字節,之前碰到位置參數時,數字代表的是第幾個字節數。請記住這種用法,也記住這個疑問。
return word
def print_last_word(words):
"""Prints the last word after popping it off."""
word =words.pop(-1)
return word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted wordd."""
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)
return print_first_word(words)
return 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)
return print_first_word(words)
return print_last_word(words)
sentence = 'All good things come to those who wait'
words = break_words(sentence)
print words
sorted_words = sort_words(words)
print sorted_words
print print_first_word(words)
print print_last_word(words)
print words
print print_first_word(sorted_words)
print print_last_word(sorted_words)
print sorted_words
sorted_words = sort_sentence(sentence)
print sorted_words
print print_first_and_last(sentence)
print print_first_and_last_sorted(sentence)
結果
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait']
['All', 'come', 'good', 'things', 'those', 'to', 'wait', 'who']
All
wait
['good', 'things', 'come', 'to', 'those', 'who']
All
who
['come', 'good', 'things', 'those', 'to', 'wait']
['All', 'come', 'good', 'things', 'those', 'to', 'wait', 'who']
All
All
我們來逐行分析一下每一步實現的是什么:
�6�1 在第 5 行你將自己的 ex25.py 執行了 import ,和你做過的其它 import 一樣。在 import 的時候你不需要加 .py 后綴。這個過程里,你把 ex25.py 當做了一個“模組 (module)” 來使用,你在這個模組里定義的函數也可以直接調用出來。
�6�1 第 6 行你創建了一個用來處理的“句子 (sentence)” 。
�6�1 第 7 行你使用 ex25 調用你的第一個函數 ex25.break_words 。其中的 . (dot, period)符號可以告訴 Python :“嗨,我要運行 ex25 里的哪個個叫 break_words 的函數!”
�6�1 第 8 行我們只是輸入 words ,而 python 將在第 9 行打印出這個變量里邊有什么。看上去可能會覺得奇怪,不過這其實是一個“列表 (list)” ,你會在后面的章節中學到它。
�6�1 10-11 行我們使用 ex25.sort_words 來得到一個排序過的句子。
�6�1 13-16 行我們使用 ex25.print_first_word 和 ex25.print_last_word 將第一個和最后一個詞打印出來。
�6�1 第 17 行比較有趣。我把 words 變量寫錯成了 wrods ,所以 python 在 18-20 行給了一個錯誤信息。
�6�1 21-22 行我們打印出了修改過的詞匯列表。第一個和最后一個單詞我們已經打印過了,所以在這里沒有再次打印出來。
剩下的行你需要自己分析一下,就留作你的加分習題了。
加分習題
1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 ex25中定義的函數。
2. 試著執行 help(ex25) 和 help(ex25.break_words) 。這是你得到模組幫助文檔的方式。所謂幫助文檔就是你定義函數時放在 """ 之間的東西,它們也被稱作 documentation comments (文檔注解),后面你還會看到更多類似的東西。
3. 重復鍵入 ex25. 是很煩的一件事情。有一個捷徑就是用 from ex25 import * 的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。
4. 把你腳本里的內容逐行通過 python 編譯器執行,看看會是什么樣子。你可以執行 CTRL-D(Windows 下是 CTRL-Z) 來關閉編譯器。
常見問題回答
有的函數打印出來的結果是 None 。
也許你的函數漏寫了最后的 return 語句。回到代碼中檢查一下是不是每一行都寫對了。
輸入 import ex15 時顯示 -bash: import: command not found 。
注意看《你應該看到的結果》部分。我是在 Python 中寫的這句,不是在命令行終端直接寫的。你要先運行 python 再輸入代碼。
輸入 import ex25.py 時顯示 ImportError: No module named ex25.py 。
.py 是不需要的。 Python z 知道文件是 .py 結尾,所以只要輸入 import ex25 即可。
運行時提示 SyntaxError: invalid syntax 。
這說明你在提示的那行有一個語法錯誤,可能是漏了半個括號或者引號,也可能識別的。一旦看到這種錯誤,你應該去對應的行檢查你的代碼,如果這行沒問題,就倒著繼續往上檢查每一行,直到發現問題為止。
函數里的代碼不是只在函數里有效嗎?為什么 words.pop(0) 這個函數會改變 words 的內容?
這個問題有點復雜,不過在這里 words 是一個列表,你可以對它進行操作,操作結果也可以被保存下來。這和你操作文件時文件的 f.readline() 工作原理差不多。
函數里什么時候該用 print ,什么時候該用 return ?
print 只是屏幕輸出而已,你可以讓一個函數既 print 又返回值。當你明白這一點后,你就知道這個問題其實沒什么意義。如果你想要打印到屏幕,那就使用 print ,如果是想返回值,那就是用 return 。
總結
以上是生活随笔為你收集整理的《笨办法学python》6_笨办法学Python 习题 25: 更多更多的练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lol新版妖姬怎么玩
- 下一篇: win10控制面板打不开怎么办 控制面板