【发布时间】:2018-12-14 09:21:58
【问题描述】:
该函数用于操作存储为文件的字典中的值。我得到了
“赋值前引用的局部变量‘json’”
在第四行,“dictio = json.loads()”。
我已经导入了 json,实际上下面的函数运行得非常好,几乎完成了相同的任务。与后面的函数不同,这个函数也抛出了一个空队列错误,尽管我没有故意要求多线程。
def updateTally(lefty): #Tally records responses to each question
global num, total, thisQ
rf = open("20QTally.json", "r")
dictio = json.loads(rf.read())
rf.close()
dictio[str(0)] += 1
total = dictio[str(0)]
if lefty == 1:
dictio[str(num)] +=1
thisQ = dictio[str(num)]
json = json.dumps(dictio)
wf = open("20QTally.json", "w")
wf.write(json)
wf.close()
def record_score(score): # 打开、读取、写入、关闭score文件,20QScores.txt(文件名) 全球总计 scoref = open(filename, "r") # 记录并报告个人的反应 sf = json.loads(scoref.read()) # Json 帮助字符串和整数写入和读取文件。 键= str(分数) 如果键入 sf: sf[键] += 1 别的: sf[key] = 1 #etc.
【问题讨论】:
标签: json variables scope local