简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM