2012-12-05 44 views
-1

下面是代碼:嘗試後在python

aList = [] 
for p in anotherList: 
    aList.append(p) 
    try: 
    k=p.someMethod() 
    aList.append(k) #getting error here 
    except someException: 
    continue 
return aList 

我得到「全球名稱錯誤 - ALIST不defined.Why所以

+0

這是您正在使用的確切代碼嗎?縮進已關閉。 – Blender

+0

它並不是確切的代碼。但是與我現在使用的類似。 – MBanerjee

+0

除了縮進之外,我沒有看到任何明顯的錯誤。如果沒有其他人發現任何東西,那麼也許你可以嘗試做一個完全可重現的問題樣本? (我建議這樣做,因爲它看起來已經差不多了)。例如,在示例的開始處用dummy數據定義anotherList,併爲someMethod使用真實方法,併爲someException實例。如果您精心構造的樣本沒有出現問題,那麼您就理解錯誤的中途 - 如果確實如此,您可以張貼樣本並確保找到幫助。 –

回答

1

如果沒有定義aList,則應該在循環的頂部收到aList.append(p)上的錯誤,然後在try-except子句中使用aList.append(k)。你確定你沒有錯字嗎?

aList = [] 
for p in anotherList: 
    aList.append(p) # <== should have gotten error here first! 
    try: 
    k=p.someMethod() 
    aList.append(k) #getting error here 
    except someException: 
    continue 
return aList 
+0

thanks.it was確實是一個錯字。 – MBanerjee

0

有什麼錯碼

>>> anotherList = [1, 2, 3, 4, 5] 
>>> aList = [] 
>>> for p in anotherList: 
...  aList.append(p) 
...  try: 
...    aList.append(9) 
...  except someException: 
...    continue 
... 
>>> aList 
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9] 

正如你所看到的,它的工作原理。

如果您還有問題,請發表您更多的代碼中,有一個在部分沒有錯誤你發佈了。

+0

是的。這是一個錯字。:): – MBanerjee