-1

コードは次のとおりです。

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

「グローバル名エラー -- aList が定義されていません。なぜですか?

4

2 に答える 2

1

が定義されていない場合は、try-except句に入る前に、ループの先頭でaListエラーを受け取っているはずです。タイプミスはありませんか?aList.append(p)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
于 2012-12-05T19:48:46.153 に答える
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]

ご覧のとおり、動作します。

それでも問題が解決しない場合は、コードをさらに投稿してください。投稿した部分にエラーはありません。

于 2012-12-05T19:46:04.830 に答える