练习25中编写的函数之一为:
def sort_words(words):
"""Sorts the words."""
return sorted(words)
不幸的是,按照练习说明在python内部调用此函数时:
sorted_words = ex25.sort_words(words)
以下错误结果:
>>> sorted_words = ex25.sort_words(words)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\tamma\desktop\Python Projects\ex25.py", line 8, in sort_words
return sorted(words)
TypeError: 'function' object is not iterable
我一直在尝试使用排序功能,但无济于事
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return break_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 a 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 = sorted_sentence(sentence)
print_first_word(words)
print_last_word(words)
错误消息应该在摘要中