[英]can not find the reason for 'name not defined' in python code
请原谅调试问题,一般来说是编码新手。 无法理解为什么我的代码突然无法运行。 我检查了似乎不是我的问题的错别字。
filepath = '/proper_noun.txt'
def pluralize(word):
proper_nouns = [line.strip() for line in open (filepath)]
for item in proper_nouns: ### print out the list once.
if (item==[-1]):
break;
目前在谷歌colab工作。
在这一点上,我只是试图将“proper_nouns”中的项目返回到一个列表中以使球滚动。 有任何想法吗?
print (proper_nouns)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-29-c6832e0493e8> in <module>()
----> 1 print (proper_nouns)
NameError: name 'proper_nouns' is not define
多谢你们。 我希望这个问题遵循SOF礼仪
由于您正在使用 Google Colab,我的猜测是您不小心没有从头开始运行代码(例如,如果您选择了从for item in proper_nouns:
开始的代码,并且只运行选定的部分,或者如果您拆分了程序在不同的单元格中),因此还没有定义proper_nouns。 请确保您运行所有内容并告诉我们是否是这样。
编辑:我只是想到了另一个选择: pluralize
function 中的行print(proper_nouns)
是吗? 如果不是,则proper_nouns的proper_nouns
为function,在function之外没有定义是正常的。 要从外部访问它,您必须在 function 之外声明它,或者返回它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.