Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

list - can not find the reason for 'name not defined' in python code

Excuse the debugging question, new to coding in general. Cannot understand why my code suddenly wont run. I have checked for typos which seems to not be my problem.

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;

currently working in google colab.

At this point, I'm just trying to return the items from 'proper_nouns' into a list to get the ball rolling. Any ideas?

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

Thanks guys. I hope this question follows SOF etiquette


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you are working on Google Colab, my guesss is that you accidentally don't run the code from the beginning (from example if you selected the code starting from for item in proper_nouns: and only run the selected part, or if you split your program in different cells), and therefore proper_nouns is not defined yet. Please make sure you run everything and tell us if that was it.

EDIT: I just thought of another option: is the line print(proper_nouns) in the pluralize function? If not, the scope of proper_nouns being the function, it's normal that it is not defined outside of the function. To access it from the outside, you must either declare it outside the function, or return it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...