欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

習(xí)題 25: 更多更多的練習(xí)?

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

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

 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
30
31
32
33
34
35
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é)完成這節(jié)練習(xí)。

你應(yīng)該看到的結(jié)果?

這節(jié)練習(xí)我們將在你之前用來做算術(shù)的 python 編譯器里,用交互的方式和你的.py 作交流。

這是我做出來的樣子:

 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
30
31
32
33
34
35
36
37
38
39
$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] 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.
>>> wrods
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'wrods' is not defined
>>> 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
$

我們來逐行分析一下每一步實(shí)現(xiàn)的是什么:

  • 在第 5 行你將自己的 ex25.py 執(zhí)行了 import,和你做過的其它 import 一樣。在 import 的時(shí)候你不需要加 .py 后綴。這個(gè)過程里,你把 ex25.py 當(dāng)做了一個(gè)“模組(module)”來使用,你在這個(gè)模組里定義的函數(shù)也可以直接調(diào)用出來。
  • 第 6 行你創(chuàng)建了一個(gè)用來處理的“句子(sentence)”。
  • 第 7 行你使用 ex25 調(diào)用你的第一個(gè)函數(shù) ex25.break_words。其中的 . (dot, period)符號可以告訴 Python:“嗨,我要運(yùn)行 ex25 里的哪個(gè)個(gè)叫 break_words 的函數(shù)!”
  • 第 8 行我們只是輸入 words,而 python 將在第 9 行打印出這個(gè)變量里邊有什么??瓷先タ赡軙?huì)覺得奇怪,不過這其實(shí)是一個(gè)“列表(list)”,你會(huì)在后面的章節(jié)中學(xué)到它。
  • 10-11 行我們使用 ex25.sort_words 來得到一個(gè)排序過的句子。
  • 13-16 行我們使用 ex25.print_first_wordex25.print_last_word 將第一個(gè)和最后一個(gè)詞打印出來。
  • 第 17 行比較有趣。我把 words 變量寫錯(cuò)成了 wrods,所以 python 在 18-20 行給了一個(gè)錯(cuò)誤信息。
  • 21-22 行我們打印出了修改過的詞匯列表。第一個(gè)和最后一個(gè)單詞我們已經(jīng)打印過了,所以在這里沒有再次打印出來。

剩下的行你需要自己分析一下,就留作你的加分習(xí)題了。

加分習(xí)題?

  1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認(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)于說:“我要把 ex25 中所有的東西 import 進(jìn)來?!背绦騿T喜歡說這樣的倒裝句,開一個(gè)新的會(huì)話,看看你所有的函數(shù)是不是已經(jīng)在那里了。
  4. 把你腳本里的內(nèi)容逐行通過 python 編譯器執(zhí)行,看看會(huì)是什么樣子。你可以執(zhí)行CTRL-D (Windows 下是 CTRL-Z)來關(guān)閉編譯器。

Project Versions

Table Of Contents

Previous topic

習(xí)題 24: 更多練習(xí)

Next topic

習(xí)題 26: 恭喜你,現(xiàn)在可以考試了!

This Page