Search code examples
pythonkeyerrormcedit

KeyError: 'Key x not found.'


I have this code from a MCEDit filter I was editing:

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)

And I get this error:

'KeyError: 'Key x not found.'

Please help!

EDIT:

Fixed, thanks @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)

Solution

  • 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)
    

    Got the answer, thanks to @Texelelf on Twitter