我尝试运行此脚本时出现此错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'print_first_word' is not defined
我不确定这是什么意思,因为当我import E25; dir(E25)
运行时print_first_word
会在dir(E25)
结果中列出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 words after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
""" prints the last words 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 = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
import E25
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)
。
我在Windows上运行python 2.7.9
{{1}}
答案 0 :(得分:1)
您已导入E25
,但您使用的ex25.prin....
与E25.print...
不同。
根据您的错误,虽然您没有使用E25或ex25来调用该功能。
您可能还想将words = stuff.split(" ")
更改为words = stuff.split()
。