ex25.py
代码
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)
'''-----------------------------------------------------------------------------'''
'''
在 python3.6 翻译器(interpreter)里与 ex25.py 文件交互,之前我们在做计算的时候也交互过。
你在终端里这样运行 python3.6(Windows 下直接输入 python ):
1 import ex25
2 sentence = "All good things come to those who wait."
3 words = ex25.break_words(sentence)
4 words
5 sorted_words = ex25.sort_words(words)
6 sorted_words
7 ex25.print_first_word(words)
8 ex25.print_last_word(words)
9 words
10 ex25.print_first_word(sorted_words)
11 ex25.print_last_word(sorted_words)
12 sorted_words
13 sorted_words = ex25.sort_sentence(sentence)
14 sorted_words
15 ex25.print_first_and_last(sentence)
16 ex25.print_first_and_last_sorted(sentence)
-------------------------------------------------------------------------------
Help on module ex25:
FUNCTIONS
break_words(stuff)
This function will break up words for us.
split() 通过指定分隔符对字符串进行切片.如果参数 num 有指定值,则分隔 num+1
个子字符串;返回分割后的字符串列表。
sort_words(words)
Sorts the words.
sorted() 函数对所有可迭代的对象进行排序操作。
返回重新排序的列表。
pop() 方法删除字典给定键 key 及对应的值,返回值为被删除的值。key 值必须给出。
否则,返回 default 值。
print_first_and_last(sentence)
Prints the first and last words of the sentence.
print_first_and_last_sorted(sentence)
Sorts the words then prints the first and last one.
print_first_word(words)
Prints the first word after popping it off.
print_last_word(words)
Prints the last word after popping it off.
sort_sentence(sentence)
Takes in a full sentence and returns the sorted words
sort_words(words)
Sorts the words.
1. 弄明白“你会看到”中各行的作用是什么,确保你理解你是如何在 ex25 模块中运行你的函数的。
2. 试试输入 help(ex25) 以及 help(ex25.break_words) (要在交互练习后输入,否则无法成
功运行)。注意你是如何获取到关于这个模块的帮助的,以及帮助是如何放在 ex25 的每一个函数
后面的 """ 字符串里的。 这些特殊的字符串被称为文件注释,我们会在后面看到更多。
3. 输入 ex25. 很无聊,可以走个捷径: from ex25 import * ,意思就是从 ex25 导入所有东
西,程序员总喜欢倒着说。打开一个新会话,看看你的函数会如何。
4. 试着拆解你的文件,看看当你用它的时候,它在 Python 里是什么样的。你得先输入 quit()
来退出 python,再重新加载它。
'''
运行结果
问题
有些函数我什么都没打印出来。
你可能有些函数忘了在后面输入 return 。检查一遍你的代码,确保每一行都是对的。
当我输入 import ex25 之后,我收到了 -bash: import: command not found.
注意看“你会看到”部分我是怎么做的。我是在 Python 里面运行的,而不是在 Terminal,也就是说,你得先运行 Python。
当我输入 import ex25.py 时收到了这样的错误: ImportError: No module named ex25 。
不要在后面加 .py ,Python 知道文件是以 .py 结尾的,所以你只用输入 ex25 即可。
我运行的时候遇到了这个错误: SyntaxError: invalid syntax 。
这意味着你漏掉了某些东西,比如少了一个 " 或者类似一对的符号。任何时候你只要收到这样的报错信息,你就从它提到的错的那行开始检查,看是不是所有字符都输入正确了,然后再回过头检查这一行上面的行是不是都输入正确了。
words.pop 函数是如何改变 words 变量的?
这是个很复杂的问题,但是在本例中 words 是一个列表,正因为如此你可以给它一些命令。这就类似于当你操作文件和很多其他东西时候它们是如何运行的一样。
在函数里我什么时候应该用 print 而不是 return 呢?
通过函数, return 能够给调用这个函数的那行代码返回一个结果,你可以把函数当成通过参数获取输入通过 return 返回输出。 print 跟这个就完全不相关了,它只是把输出结果打印到终端。