2017-02-28 54 views
-1

我试图执行此代码之前引用,UnboundLocalError:局部变量“Z”分配

def getInterPropreCat(h,cat): 
    for txt in h: 
     if txt['Categorie'] == cat: 
      z=[txt['Max'],txt['Min']] 
    g=getListZERO(h,cat) 
    for text in g: 
     z = getInterPropore(text['Min'],text['Max'],z[1],z[0]) 
     # print(y) 
    return z 

和我做一个测试:

print(getInterPropreCat(h,cat1)) 这是给我一个结果,那么我做一个循环让所有类别的结果:

for txt in CatList: 
    getInterPropreCat(h,txt) 

,我有这样的错误:

z = getInterPropore(text['Min'],text['Max'],z[1],z[0]) 

UnboundLocalError:局部变量 'Z' 分配

任何帮助之前引用?

+0

如果'h'中的字符没有匹配的'Categorie',那么'z'永远不会被设置。 – chepner

+0

你必须知道我的函数给出[3000.0,1746.65],这对我很有用......但是当我在CatList中使用txt时:getInterPropreCat(h,txt)..给出错误 –

+0

提供一个具体的例子(CatList:getInterPropreCat(h,txt))中的'CatList'和'h'的值会产生错误。 – chepner

回答

0

你得到的错误,因为你没有在你的第一条if语句中设置任何值,因为if语句要求不匹配,请确保'txt ['Categorie'] == cat'返回True,这样一些东西被填充在z列表中。

+0

它返回True ..并且代码适用于我调用函数print(getInterPropreCat( h,cat1))..但是在使用循环之后,它给出错误 –

+0

是z [0]和z [1]有值?在执行'getInterPropore'之前添加类似print(z [0])的值 – ali

+0

它的值为z = [txt ['Max'],txt ['Min']] –

相关问题