🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 习题 25: 更多更多的练习 我们将做一些关于函数和变量的练习,以确认你真正掌握了这些知识。这节练习对你来说可以说是一本道:写程序,逐行研究,弄懂它。 不过这节练习还是有些不同,你不需要运行它,取而代之,你需要将它导入到 python 里通过自己执行函数的方式运行。 <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23&#13; 24&#13; 25&#13; 26&#13; 27&#13; 28&#13; 29&#13; 30&#13; 31&#13; 32&#13; 33&#13; 34&#13; 35</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>def break_words(stuff):&#13; """This function will break up words for us."""&#13; words = stuff.split(' ')&#13; return words&#13; &#13; def sort_words(words):&#13; """Sorts the words."""&#13; return sorted(words)&#13; &#13; def print_first_word(words):&#13; """Prints the first word after popping it off."""&#13; word = words.pop(0)&#13; print word&#13; &#13; def print_last_word(words):&#13; """Prints the last word after popping it off."""&#13; word = words.pop(-1)&#13; print word&#13; &#13; def sort_sentence(sentence):&#13; """Takes in a full sentence and returns the sorted words."""&#13; words = break_words(sentence)&#13; return sort_words(words)&#13; &#13; def print_first_and_last(sentence):&#13; """Prints the first and last words of the sentence."""&#13; words = break_words(sentence)&#13; print_first_word(words)&#13; print_last_word(words)&#13; &#13; def print_first_and_last_sorted(sentence):&#13; """Sorts the words then prints the first and last one."""&#13; words = sort_sentence(sentence)&#13; print_first_word(words)&#13; print_last_word(words)&#13; </pre> </div> </td> </tr></tbody></table> 首先以正常的方式 pythonex25.py 运行,找出里边的错误,并把它们都改正过来。然后你需要接着下面的答案章节完成这节练习。 ### 你应该看到的结果 这节练习我们将在你之前用来做算术的 python 编译器里,用交互的方式和你的.py 作交流。 这是我做出来的样子: <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23&#13; 24&#13; 25&#13; 26&#13; 27&#13; 28&#13; 29&#13; 30&#13; 31&#13; 32&#13; 33&#13; 34&#13; 35&#13; 36&#13; 37&#13; 38&#13; 39</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>$ python&#13; Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) &#13; [GCC 4.0.1 (Apple Inc. build 5465)] on darwin&#13; Type "help", "copyright", "credits" or "license" for more information.&#13; &gt;&gt;&gt; import ex25&#13; &gt;&gt;&gt; sentence = "All good things come to those who wait."&#13; &gt;&gt;&gt; words = ex25.break_words(sentence)&#13; &gt;&gt;&gt; words&#13; ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']&#13; &gt;&gt;&gt; sorted_words = ex25.sort_words(words)&#13; &gt;&gt;&gt; sorted_words&#13; ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']&#13; &gt;&gt;&gt; ex25.print_first_word(words)&#13; All&#13; &gt;&gt;&gt; ex25.print_last_word(words)&#13; wait.&#13; &gt;&gt;&gt; wrods&#13; Traceback (most recent call last):&#13; File "&lt;stdin&gt;", line 1, in &lt;module&gt;&#13; NameError: name 'wrods' is not defined&#13; &gt;&gt;&gt; words&#13; ['good', 'things', 'come', 'to', 'those', 'who']&#13; &gt;&gt;&gt; ex25.print_first_word(sorted_words)&#13; All&#13; &gt;&gt;&gt; ex25.print_last_word(sorted_words)&#13; who&#13; &gt;&gt;&gt; sorted_words&#13; ['come', 'good', 'things', 'those', 'to', 'wait.']&#13; &gt;&gt;&gt; sorted_words = ex25.sort_sentence(sentence)&#13; &gt;&gt;&gt; sorted_words&#13; ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']&#13; &gt;&gt;&gt; ex25.print_first_and_last(sentence)&#13; All&#13; wait.&#13; &gt;&gt;&gt; ex25.print_first_and_last_sorted(sentence)&#13; All&#13; who&#13; &gt;&gt;&gt; ^D&#13; $&#13; </pre> </div> </td> </tr></tbody></table> 我们来逐行分析一下每一步实现的是什么: - 在第 5 行你将自己的 ex25.py 执行了 import,和你做过的其它 import 一样。在 import 的时候你不需要加 .py 后缀。这个过程里,你把 ex25.py 当做了一个“模组(module)”来使用,你在这个模组里定义的函数也可以直接调用出来。 - 第 6 行你创建了一个用来处理的“句子(sentence)”。 - 第 7 行你使用 ex25 调用你的第一个函数 ex25.break_words。其中的 . (dot, period)符号可以告诉 Python:“嗨,我要运行 ex25 里的哪个个叫 break_words 的函数!” - 第 8 行我们只是输入 words,而 python 将在第 9 行打印出这个变量里边有什么。看上去可能会觉得奇怪,不过这其实是一个“列表(list)”,你会在后面的章节中学到它。 - 10-11 行我们使用 ex25.sort_words 来得到一个排序过的句子。 - 13-16 行我们使用 ex25.print_first_word 和 ex25.print_last_word 将第一个和最后一个词打印出来。 - 第 17 行比较有趣。我把 words 变量写错成了 wrods,所以 python 在 18-20 行给了一个错误信息。 - 21-22 行我们打印出了修改过的词汇列表。第一个和最后一个单词我们已经打印过了,所以在这里没有再次打印出来。 剩下的行你需要自己分析一下,就留作你的加分习题了。 ### 加分习题 1. 研究答案中没有分析过的行,找出它们的来龙去脉。确认自己明白了自己使用的是模组 ex25 中定义的函数。 1. 试着执行 help(ex25) 和 help(ex25.break_words) 。这是你得到模组帮助文档的方式。 所谓帮助文档就是你定义函数时放在 """ 之间的东西,它们也被称作 documentationcomments (文档注解),后面你还会看到更多类似的东西。 1. 重复键入 ex25. 是很烦的一件事情。有一个捷径就是用 fromex25import* 的方式导入模组。这相当于说:“我要把 ex25 中所有的东西 import 进来。”程序员喜欢说这样的倒装句,开一个新的会话,看看你所有的函数是不是已经在那里了。 1. 把你脚本里的内容逐行通过 python 编译器执行,看看会是什么样子。你可以执行CTRL-D (Windows 下是 CTRL-Z)来关闭编译器。