首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

local variable 'xxx'referenced before assignment

2013-01-01 
【求助】local variable 'xxx'referenced before assignmentdef a(): test{a:123,b:456}for

【求助】local variable 'xxx'referenced before assignment

def a(): 
test={'a':123,'b':456}
for item in test:
 if item.name == 'a':
  xxx=item.name+'='+item.value
a=xxx
def b()
……

xxx这个变量在引用前还没有定义,这上面不是定义了么?
如果去掉 def b()就正常了,求解。
[解决办法]
# python version:3.2
# -*- coding:gbk -*-

test = {'a':123, 'b':456}
for item in test:
    if item == 'a':
        xxx = item + '=' + str(test[item])

[解决办法]
def a():
    test = {'a':123,'b':456}
    for item in test:
        if item == 'a':
            xxx = item + '=' + str(test[item])
            return xxx


def a():
    test = {'a':123,'b':456}
    for item in test:
        if item == 'a':
            return item + '=' + str(test[item])


[解决办法]
很明显的变量作用域的问题啊。。。
def a(): 
test={'a':123,'b':456}
xxx = None
for item in test:
 if item.name == 'a':
  xxx=item.name+'='+item.value
a=xxx
def b()
……

热点排行