【问题标题】:KeyError: 'Key x not found.'KeyError:“未找到密钥 x。”
【发布时间】:2014-04-13 19:27:50
【问题描述】:

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

【问题讨论】:

  • 错误是哪一行?我假设它位于if "x" in t: del t["x"]。你能打印出level.tileEntityAt(x, y, z) 做了什么吗?
  • 其实在line: t = level.tileEntityAt(x, y, z),我真不知道MCEdit能不能打印出什么"level.tileEntityAt(x, y, z)"是的,对不起。
  • 如果您有解决方案,您应该将其作为答案发布并标记为已接受。可以回答您自己的问题。
  • @Mike W 好的,做到了,谢谢

标签: python keyerror mcedit


【解决方案1】:

这是你的功能:

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)

在代码的第二行中,您在 for 循环中分配 x。然后您继续调用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)

【讨论】:

  • 这行不通:循环变量xyz 将不会被使用。
  • 现在我得到一个缩进错误:` if t and t["id"].value == "Control": ^ IndentationError: unexpected indent`
  • 那是因为你原来的缩进关闭了。检查我编辑的代码。
  • 您不认为 OP 需要将 x, y, z 参数作为整数吗? (不知道,也许不是)
  • 问题不是很清楚,downvoter,为什么要downvote?
【解决方案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)

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

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 2017-03-15
    • 2013-10-17
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多