local variable 'Postive_Points' referenced before assignment
我使用的是Jypyter笔记本电脑,我的代码对某些输入不断给出相同的错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | file0 = open("stopwords.txt","r") StopWords=list(file0.read().split(' ')) file = open("positive-words.txt","r") positive_words=list(file.read().split(' ')) file2 = open("negative-words.txt","r") negative_words=list(file2.read().split(' ')) def evaluate(string,Positive_Points=0,Negative_Points=0): string=list(string.split()) ### print(string) Output=[] for s in string: s=s.lower() if s not in StopWords: Output.append(s) ### print(Output) for word in Output: ### print(word) if word in positive_words: Postive_Points+=1 elif word in negative_words: Negative_Points+=1 return Positive_Points,Negative_Points |
Jupyter笔记本中的下一个单元:
1 2 3 4 5 6 | import matplotlib.pyplot as plt result = evaluate("Everything is nice") lables = ['Positive','Negative'] colors = ['blue','red'] plt.pie(result, labels=lables, colors=colors, startangle=90, autopct='%.1f%%') plt.show() |
这就是错误看起来的样子:
UnboundLocalError回溯(最近调用的最后一个)在里面
1 2 3 4 5 | 1 import matplotlib.pyplot as plt 2 ----> 3 result = evaluate("Everything is nice") 4 lables = ['Positive','Negative'] 5 colors = ['blue','red'] |
在评估中(字符串、正点、负点)
1 2 3 4 5 | 21 ### print(word) 22 if word in positive_words: ---> 23 Postive_Points+=1 24 elif word in negative_words: 25 Negative_Points+=1 |
UnboundLocalError:分配前引用的局部变量"postive_points"
你刚才在23行把