尝试运行几天前可以运行的代码时遇到了一个奇怪的错误。添加了一点根本不使用str()
的小功能
结果为UnboundLocalError:赋值之前引用了本地变量'str'
。引用的部分完全没有被触及。
这是片段。 (如果需要我的更多代码,我会编辑我的帖子):
def get_level_stats(number,version,prev_uniques):
starts = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in": version }, "status": "start"})
fails = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in": version }, "status": "failed"})
finishes = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in":version }, "status": "finish"})
players_dict = {}
for result in starts:
try:
players_dict[result['uid']]
players_dict[result['uid']]["starts"] = players_dict[result['uid']]["starts"] +1
players_dict[result['uid']]["last_day"] = result['updated_at']
except:
players_dict[result['uid']] = {}
players_dict[result['uid']]["starts"] = 1
players_dict[result['uid']]["finish"] = 0
players_dict[result['uid']]["booster"] = 0
players_dict[result['uid']]["extra"] = 0
players_dict[result['uid']]["last_day"] = result['updated_at']
for result in finishes:
try:
players_dict[result['uid']]
players_dict[result['uid']]["finish"] = players_dict[result['uid']]["finish"] +1
players_dict[result['uid']]["bubbles_left"] = result['bubbles_left']
if int(result['use_special']) >= 1:
players_dict[result['uid']]["booster"] = players_dict[result['uid']]["booster"] + 1
except:
players_dict[result['uid']] = {}
players_dict[result['uid']]["finish"] = 0
print(players_dict)
try:
dificulty = 1.-finishes.count()/starts.count()
except:
dificulty = 1
uniques = len(players_dict.keys())
if number >= 2:
dropedPeople = prev_uniques/ uniques
else:
dropedPeople = 0
test = players_dict
for fin in finishes:
test[result['uid']]["starts"] = 0
if fin['updated_at'] < test['last_day']:
test[fin['uid']]['last_day'] = fin['updated_at']
for str in starts:
if str['updated_at'] < test['last_day']:
test[result['uid']]["starts"] = test[result['uid']]["starts"] + 1
attemps = 0
for player in test:
attemps = players_dict[player]["starts"] + attemps
average = attemps/uniques
win_in_first_atempt = 0
lost_people_count = 0
bubbles_left = [0]
bubbles_left_first_try = [0]
fails_count = [0]
fails_count_lost_people = [0]
lost_percent = 0
for player in players_dict:
try:
if (players_dict[player]["finish"] == 1 and players_dict[player]["starts"] == 1):
win_in_first_atempt = win_in_first_atempt+1
if players_dict[player]["finish"] >= 1:
if players_dict[player]["bubbles_left"].isdigit():
bubbles_left_first_try.append(players_dict[player]["bubbles_left"])
delta = datetime.datetime.now() - players_dict[player]["last_day"]
if (delta.days > 2 and players_dict[player]["finish"] == 0) :
lost_people_count = lost_people_count + 1
lost_percent = lost_people_count/uniques
if players_dict[player]["finish"] >= 1:
if players_dict[player]["bubbles_left"].isdigit():
bubbles_left.append(players_dict[player]["bubbles_left"])
if players_dict[player]["starts"] > 1:
fails_count.append(players_dict[player]["starts"] - 1)
except:
print("some erroe")
try:
win_percent = win_in_first_atempt/uniques
except:
win_percent = 0
bubbles_left_first_try = list(map(int, bubbles_left_first_try))
bubbles_left_first_try.sort()
bubbles_left = list(map(int, bubbles_left))
bubbles_left.sort()
median_left = statistics.median(bubbles_left)
percentile25_bubbles = np.percentile(bubbles_left, 25)
percentile75_bubbles = np.percentile(bubbles_left, 75)
fails_count = list(map(int, fails_count))
fails_count.sort()
fails_median = statistics.median(fails_count)
fails_percentile75 = np.percentile(fails_count, 75)
ReturnList = [
uniques,
number, uniques,
starts.count(),
finishes.count(),
fails.count(),
round(dificulty*100, 2),
win_in_first_atempt,
round(dropedPeople*100, 2),
median_left,
percentile25_bubbles,
percentile75_bubbles,
fails_median,
fails_percentile75,
average]
return ReturnList
prev = 0
for number in range(1,3):
res = get_level_stats(number,version,prev)
lol = res.pop(0)
print(res)
答案 0 :(得分:0)
发生此错误是因为Python认为str
是局部变量。这意味着您稍后会在同一函数中使用名称str
来保存值。
例如:
>>> def test():
... str(3)
... str = 'test'
...
>>> test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in test
UnboundLocalError: local variable 'str' referenced before assignment
由于代码已分配给str
,因此Python编译器假定它是该范围内的局部变量,但是在为局部变量赋值之前不能使用它,因此是例外。解决方案是在代码中搜索str
的出现,如果它被用作变量名,则重命名该变量。
答案 1 :(得分:0)
我认为这行是错误的。
for str in starts:
尝试将其更改为
for strs in starts: