-1

我从我正在编辑的 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)
4

2 回答 2

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)

在代码的第二行中,您xfor循环中分配。然后您继续调用t = level.tileEntityat(x, y, z),这意味着您正在寻找 的值x,如您的第二行中所定义。相反,将您的x, y, 和z引号括起来,使它们成为字符串。我不完全确定这是你想要的,因为我不知道里面有什么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)
于 2014-04-13T19:38:13.323 回答
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

于 2014-04-16T01:04:20.960 回答