2013-12-17 61 views
-2

我在一臺機器上獲得「在分配」之前引用了「UnboundLocalError:本地變量」的python腳本,但在腳本運行正常的另一臺機器上沒有。這兩臺機器都在Windows 7上並使用Python 2.7.3。任何建議可能是這種行爲的原因?謝謝。 下面是導致該錯誤代碼:Python在一臺機器上未綁定本地錯誤,但不在另一臺機器上

with open(self.temp_dir + test + ".log",'r') as log: 
     for line in log: 
     if "INPUT_SENTENCE" in line: 
      match = patternInput.match(line) 
      inputSentence = match.group(1).lower() 
      if inputSentence in self.testToDiagDict[test]: 
      continue 
      self.testToDiagDict[test][inputSentence] = [] 
     if "STATS" in line: 
      if "Input Sentences" in line: 
      inputSentences = patternValue.findall(line) 
      self.testToDiagDict[test][inputSentence].append(inputSentences[0]) 

和Trace:

File "C:\X\Y\X\script.py", line 90, in extract_data 
    if "Input Sentences" in line: 
UnboundLocalError: local variable 'inputSentence' referenced before assignment 
+1

'UnboundLocalError' is一個*運行時*異常,而不是語法錯誤。我們需要查看您的代碼才能提供幫助。 –

+0

請提供更多的細節 - 一些代碼會很好,並追溯。 – matino

+0

檢查它在工作的機器上是指什麼。除了錯誤以外,劇本會做什麼? – Joop

回答

0

的問題是在這條線:

self.testToDiagDict[test][inputSentence].append(inputSentences[0]) 

則存在範圍不inputSentence

因此,代碼可能已經在一個機器貝科工作使用此if statement

if "INPUT_SENTENCE" in line: 

評估爲真,但它不移動的情況,那就是當你得到這個錯誤。我無法提出解決方案,因爲我不知道代碼的其餘部分是什麼樣的,或者你正在努力完成什麼,但我已經指出的應該允許你重新評估你的設計

+0

它應該總是拋出錯誤,但它不會。 – maddy

+0

這很有道理!謝謝! – maddy

0

也許正如@ inspectorG4dget說 - 你有不同的代碼路徑的地方。下面是一個簡單的例子,可能導致你錯誤

def f(): 
    if machine1: 
     s = 1 
    else 
     S = 2 
    print s 

如果你想顯示的代碼,它可能會花費幾秒鐘找到

相關問題