先建立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):
"""Print 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)
运行代码如下:
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)
结果如下:
All
wait.
All
who
All
wait.
All
who
进程已结束,退出代码0
巩固练习
1. 使用help(ex25)和help(ex25.break_words)
帮助文档就是在定义函数时放在"""之间的字符串,也成为注释
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)
help(ex25)
print('help')
help(ex25.break_words)
print('help')
结果如下:
All
wait.
All
who
All
wait.
All
who
Help on module ex25:
NAME
ex25
FUNCTIONS
break_words(stuff)
This function will break up words for us.
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)
Print 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.
FILE
d:\\ex25.py
help
Help on function break_words in module ex25:
break_words(stuff)
This function will break up words for us.
help
进程已结束,退出代码0
Tips:
1. 有的函数打印出来是none
漏写了return,回到代码check
2. 输入import ex25时显示-bash:import:command not found.
这不是在终端运行的代码,你需要先运行python再录入代码
3. 输入import ex25时显示ImportError:No module named ex25.py
.py是不需要的,python知道文件是.py结尾,只需要输入import ex25
4. 运行时提示SyntaxError: invalid syntax
语法错误,是不是少了括号,或者其他语法问题。按照报错提示寻找语法错误
5. 为什么word.pop这个函数会改变words变量内容?
words是一个列表,可以对它进行操作,结果也可以被保存
6. 什么时候用print,什么时候用return?
return会给调用函数的代码给一个结果。思路:函数通过参数接收输入,通过return返回输出。print只是打印输出。
2278

被折叠的 条评论
为什么被折叠?



