UnboundLocalError将列表从函数传递给函数。宣布全球

时间:2014-03-19 20:09:47

标签: python

global GearsList;GearsList=[]
if len(DuplicateList)>0:
 for i in DuplicateList:
  GearsList.append(i)
  for j in UniqueList:
   GearsList.append(j)
  CheckGaps(); GearsList=[]
else:
 GearsList=UniqueList
 file=open('GearsList.csv','w')
 for z in GearsList:
  print>>file,z
 file.close()
 CheckGaps()

def CheckGaps():
 GearsList.sort(key=lambda x:x[1]); list1=[]; base=''

第一个块是成功传递给CheckGaps的函数的一部分。

但是,我收到了:

UnboundLocalError: local variable 'GearsList' referenced before assignment

我在宣布“全球GearsList”和GearsList.csv时感到困惑:

(7, 2.8238952201055705, 8, 2.563397837907839, 1.1016219091494361)
(7, 1.0953215668837057, 8, 1.256834873031462, 0.8714920236433373)

作为一项实验,我编写了以下内容,但没有错误。

def CheckRatios():
 global GearsList;GearsList=[]
 DuplicateList=[]; UniqueList=[]
 if len(DuplicateList)>0:
  for i in DuplicateList:
   GearsList.append(i)
   for j in UniqueList:
    GearsList.append(j)
   CheckGaps(); GearsList=[]
 else:
  GearsList.append((7, 2.8238952201055705, 8, 2.563397837907839, 1.1016219091494361))
  GearsList.append((7, 1.0953215668837057, 8, 1.256834873031462, 0.8714920236433373))
  file=open('GearsList.csv','w')
  for z in GearsList:
   print>>file,z
  file.close()
  CheckGaps()

def CheckGaps():
 GearsList.sort(key=lambda x:x[1]); list1=[]; base=''
 for i in GearsList:
  print i
 print "   It worked."

CheckRatios()

我必须遗漏一些明显的东西。有什么问题?

0 个答案:

没有答案