i declare global variable pos
, anyway method printarr()
doesn't see it. interpreter says: undifined variable pos
. why? thanks
import nltk class analyzer(): """implements sentiment analysis.""" def __init__(self, positives, negatives): global pos global neg positives=[] negatives=[] i=0 file = open("negative-words.txt","r") line in file: h=file.readline() if h!="": if h[0]!=';': negatives.append(h) i=i+1 print(h) i=0 file = open("positive-words.txt","r") line in file: h=file.readline() if h!="": if h[0]!=';': positives.append(h) i=i+1 print(h) neg=negatives pos=positives def printarr (self, arr): print (pos[2]) """initialize analyzer."""
i'll try make easier: next code not work properly, program still prints out "0", not "5"
import os import sys global x x=0 def func1(): x=5 def func2(): print (x) func2()
python local variables first before searching in global scope.
your func2() refer global value of x x not defined locally.
you try nest 2 functions.
func2(func1())
that way func1 assign value 5 variable x , func2 return 5 instead of 0.
hope helps.
Comments
Post a Comment