Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python语法混乱:未定义名称_Python_Syntax - Fatal编程技术网

python语法混乱:未定义名称

python语法混乱:未定义名称,python,syntax,Python,Syntax,我一直在学习“艰苦地学习Python”,这段代码让我非常头疼: def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return words def sort_words(words): """Sort the words.""" return sorted(words) def print_first_word

我一直在学习“艰苦地学习Python”,这段代码让我非常头疼:

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sort 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):
    """Prints 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 = brea_words(sentence)
    return sort_words(words)

def print_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)

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 ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ex25.py", line 4, in break_words
    return words
NameError: global name 'words' is not defined
导入ex25 >>>一切美好的事物都会降临到等待的人身上 >>>单词=ex25。断开单词(句子) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“ex25.py”,第4行,分词 回话 NameError:未定义全局名称“words”
我哪里出错了?我检查了代码数百万次…

正如我在评论中所说,我认为您的pyc文件正在导入,而不是py代码。

正如我在评论中所说,我认为您的pyc文件正在导入,而不是py代码。

您试过运行吗

reload(ex25)

??如果导入一个文件,然后更改该文件,python进程不知道您更改了它。您必须重新启动,或者让它再次查看模块。

您试过运行吗

reload(ex25)

??如果导入一个文件,然后更改该文件,python进程不知道您更改了它。您必须重新启动,或者让它再次查看模块。

假设上述答案足够了-您需要重新加载模块。在NFS文件系统上,我发现在更改python代码(并且使用了旧的pyc文件)后,没有重新编译python代码会出现问题。如果“重新加载”不起作用,您可以尝试删除pyc文件并重新启动python

  ddzialak@pld ~$ python
  Python 2.7.2 (default, Aug 16 2011, 10:09:58)
  [GCC 4.6.1 20110714 (release)] on linux2
  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)
  >>> print words
  ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']

假设上面的答案足够了-您需要重新加载模块。在NFS文件系统上,我发现在更改python代码(并且使用了旧的pyc文件)后,没有重新编译python代码会出现问题。如果“重新加载”不起作用,您可以尝试删除pyc文件并重新启动python

  ddzialak@pld ~$ python
  Python 2.7.2 (default, Aug 16 2011, 10:09:58)
  [GCC 4.6.1 20110714 (release)] on linux2
  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)
  >>> print words
  ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']

我无法复制这个。。。我不明白那个错误。为什么不让break_单词返回东西。拆分(“”)?没有解释错误,但让您更接近工作代码…注意在
sort\u句()
中,您有一个打字错误
brea\u words()
而不是
break\u words()
。想想@JakobBowyer可能是对的-这应该可以工作(尽管
sort\u句中有错误
)-因此您可能有一个旧的
.pyc
,由于某种原因没有被删除,因此没有调用正确的代码。您不需要删除
.pyc
文件。只需再次保存
.py
文件,Python就会重新编译。您的代码看起来很好,
break\u words
中不应该有错误。请确保在更改源文件后重新启动Python解释器(或使用
reload()
)。。。我不明白那个错误。为什么不让break_单词返回东西。拆分(“”)?没有解释错误,但让您更接近工作代码…注意在
sort\u句()
中,您有一个打字错误
brea\u words()
而不是
break\u words()
。想想@JakobBowyer可能是对的-这应该可以工作(尽管
sort\u句中有错误
)-因此您可能有一个旧的
.pyc
,由于某种原因没有被删除,因此没有调用正确的代码。您不需要删除
.pyc
文件。只需再次保存
.py
文件,Python就会重新编译。您的代码看起来很好,
break\u words
中不应该有错误。更改源文件后,请确保重新启动Python解释器(或使用
reload()
)。