KeyError:'未找到密钥x。'

时间:2014-04-13 19:27:50

标签: python keyerror mcedit

我从我正在编辑的MCEDit过滤器中获取此代码:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt(x, y, z)
                                if t and t["id"].value == "Control":
                                        if "id" in t: del t["id"]
                                        if "x" in t: del t["x"]
                                        if "y" in t: del t["y"]
                                        if "z" in t: del t["z"]
                                        return (t, x, y, z)
        return (None, None, None, None)

我收到了这个错误:

'KeyError: 'Key x not found.'

请帮忙!

编辑:

修正了,谢谢@Texelelf:

def getCommand(level, box):
for x in xrange(box.minx,box.maxx):
    for y in xrange(box.miny,box.maxy):
        for z in xrange(box.minz,box.maxz):
            t = deepcopy(level.tileEntityAt(x,y,z))
            if t and t["id"].value == "Control":
                if "id" in t: del t["id"]
                if "x" in t: del t["x"]
                if "y" in t: del t["y"]
                if "z" in t: del t["z"]
                return (t, x, y, z)
    return (None, None, None, None)

2 个答案:

答案 0 :(得分:0)

这是你的功能:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt(x, y, z)
                                if t and t["id"].value == "Control":
                                        if "id" in t: del t["id"]
                                        if "x" in t: del t["x"]
                                        if "y" in t: del t["y"]
                                        if "z" in t: del t["z"]
                                        return (t, x, y, z)
        return (None, None, None, None)

在代码的第二行,您在x循环中指定for。然后,您继续致电t = level.tileEntityat(x, y, z),这意味着您正在寻找第二行中定义的x的值。相反,请将xyz括在引号中,以使其成为字符串。我不完全确定这是你想要的,因为我不知道level.tileEntityAt(x, y, z)中的内容,但我做出了最好的猜测。

编辑代码:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt("x", "y", "z")
                    if t and t["id"].value == "Control":
                        if "id" in t: del t["id"]
                        if "x" in t: del t["x"]
                        if "y" in t: del t["y"]
                        if "z" in t: del t["z"]
                        return (t, x, y, z)
        return (None, None, None, None)

答案 1 :(得分:0)

def getCommand(level, box):
for x in xrange(box.minx,box.maxx):
    for y in xrange(box.miny,box.maxy):
        for z in xrange(box.minz,box.maxz):
            t = deepcopy(level.tileEntityAt(x,y,z))
            if t and t["id"].value == "Control":
                if "id" in t: del t["id"]
                if "x" in t: del t["x"]
                if "y" in t: del t["y"]
                if "z" in t: del t["z"]
                return (t, x, y, z)
    return (None, None, None, None)

得到答案,感谢Twitter上的@Texelelf