我正在使用jypyter笔记本,并且我的代码在某些输入中始终给出相同的错误:
file0 = open("stopwords.txt", "r")
StopWords=list(file0.read().split('\n'))
file = open("positive-words.txt", "r")
positive_words=list(file.read().split('\n'))
file2 = open("negative-words.txt", "r")
negative_words=list(file2.read().split('\n'))
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笔记本中的下一个单元格:
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 import matplotlib.pyplot as plt
2
----> 3 result = evaluate("Everything is nice")
4 lables = ['Positive','Negative']
5 colors = ['blue','red']
评估中的(字符串,Positive_Points,Negative_Points)
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”
答案 0 :(得分:0)
您在第23行中拼错了Positive_Points
。