当我在控制台中测试我的模块时没有给出错误,但是当我尝试从浏览器接收数据时,我收到一条错误消息:
UnboundLocalError: local variable 'mile' referenced before assignment
这是我的代码的相关部分:
while cursor.alive:
try:
doc = yield motor.Op(cursor.next_object)
if doc:
mileInfo={'time': doc['time_normal']}
print doc['term']
if doc['term'] == 'abc':
event = 'abc'
elif doc['term'] == 'def':
event = 'def'
elif doc['term'] == 'xyz':
event = 'xyz'
else:
event = 'rst'
if not doc['coordinates']:
placeName = doc['place']['full_name']
mile = from_name(placeName, event)
print 'from Name: ' ,mile, 'term: ', event, doc['place']['full_name']
else:
mile = get_coords(doc['coordinates']['coordinates'], event)
print 'from coordinates: ',mile, 'term: ', event, 'location:', doc['coordinates']['coordinates']
mileInfo['miles'] = mile
self.write_message(json.dumps(mileInfo, default = json_util.default))
except StopIteration:
pass
except:
traceback.print_exc()
我很困惑,为什么我只能从网络浏览器中收到此错误。
谢谢
答案 0 :(得分:0)
我能够通过声明一个全局变量'mile'并通过清理缩进来排序错误(感谢建议@MartijnPieters)。所以我在这里插入了一个全局变量:
while cursor.alive:
try:
doc = yield motor.Op(cursor.next_object)
global mile
if doc: ...............