KeyError: 'Key x not found.'
Pregunta
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)
Solución 2
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
Otros consejos
Here is your function:
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)
In the second line of your code, you assign x
in a for
loop. You then proceed to call t = level.tileEntityat(x, y, z)
, which means you are looking for the value of x
, as defined in your second line. Instead, surround your x
, y
, and z
in quotations, to make them strings. I am not completely sure this is what you want because I don't know what is in level.tileEntityAt(x, y, z)
, but I'm making my best guess.
Edited code:
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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow