我开始学习Python,我发现它很有趣。我正在尝试创建自己的模块,并遇到了一个碰撞。该代码是这样的:在Python中创建模块时出现错误消息
def break_words(sentence):
words = sentence.split(' ')
return words
def sort_words (words):
sort_word=sorted(words)
return sort_word
第二个功能具有由第一送入参数words
,我认为它应该工作,因为它已经返回,但在Python运行filename.sort_words(words)
,它给出了一个错误信息NameError:global name 'words' is not defined
。在它运行第二个函数之前,它要求我定义诸如words=filename.break_words(sentence)
之类的词。
我的代码有什么问题?
请不要忘记在这里正确的代码缩进。它真的提高了可视性。 – valex