习题25 更多更多的练习# 定义break_words函数def break_words(stuff): # 加注释 """This function will break up words for us.""" # 将sutff按空格分隔——words words = stuff.split(' ') return words# 定义sort_wo...
摘要由CSDN通过智能技术生成
习题25 更多更多的练习
# 定义break_words函数defbreak_words(stuff):# 加注释"""This function will break up words for us."""# 将sutff按空格分隔——words
words = stuff.split(' ')return words
# 定义sort_words函数defsort_words(words):"""sorts the words."""# 将words内的元素排序,并将排序后的内容返回returnsorted(words)# 定义print_first_words函数defprint_first_word(words):"""prints the first word after popping it off."""# 将words的第一个元素删除,并将该元素赋值给word
word = words.pop(0)print(word)# 定义print_first_words函数defprint_last_word(words):"""prints the last word after popping it off."""# 将words的最后一个元素删除,并将该元素赋值给word
word = words.pop(-1)print(word)# 定义sort_sentence函数defsort_sentence(sentence):"""Takes in a full sentence and returens the sorted words."""# sentence为参数,运行行1的函数,赋值给words
words = break_words(sentence)# words为参数,运行行11的函数,并返回return sort_words(words)# 定义print_first_and_last函数defprint_first_and_last(sentence):"""Prints the first and last words of the sentence."""# sentence为参数,运行行1的函数,赋值给words
words = break_words(sentence)# words为参数,运行行17的函数
print_first_word(words)# words为参数,运行行25的函数
print_last_word(words)# 定义print_first_and_last_sorted函数defprint_first_and_last_sorted(sentence):"""Sorts the words then prints the first and last one."""# sentence为参数,运行行33的函数,结果赋值给words
words = sort_sentence(sentence)# words为参数,运行行17的函数
print_first_word(words)# words为参数,运行行25的函数
print_last_word(words)
PS D:\pythonp> python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46)[MSC v.1916 64 bit (AMD64)] on win32
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',
习题25 更多更多的练习# 定义break_words函数def break_words(stuff): # 加注释 """This function will break up words for us.""" # 将sutff按空格分隔——words words = stuff.split(' ') return words# 定义sort_wo...