defbreak_words(stuff):"""This function will break up words for us."""words=stuff.split(' ')returnwordsdefsort_words(words):"""Sorts the words."""returnsorted(words)defprint_first_word(words):"""Prints the first word after popping it off."""word=words.pop(0)printworddefprint_last_word(words):"""Prints the last word after popping it off."""word=words.pop(-1)printworddefsort_sentence(sentence):"""Takes in a full sentence and returns the sorted words."""words=break_words(sentence)returnsort_words(words)defprint_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)defprint_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)
测试文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)