2012-11-25 246 views
-1

將數據發送到Web瀏覽器當我測試模塊中的控制檯給出沒有錯誤,但是當我嘗試從一個瀏覽器我得到一個錯誤信息接收數據:錯誤蟒蛇

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() 

我很困惑,爲什麼我只能從網絡瀏覽器得到這個錯誤。

感謝

+1

您的縮進是可怕的;這可能是你的問題的原因。 –

+0

@MartijnPieters我很驚訝它運行。對於這些內容的編譯會不會失敗? – Ian

+0

感謝您的回覆。縮進是從我的工作區複製代碼。我認爲它被改變了。這是我得到的唯一回溯。 – user94628

回答

0

我能夠通過清理縮進(感謝您的建議@MartijnPieters)聲明一個全局變量「一英里」,也到錯誤排序。所以我在這裏插入一個全局變量:

while cursor.alive: 
    try: 
     doc = yield motor.Op(cursor.next_object) 
     global mile 

     if doc: ...............