简体   繁体   English

KeyError: 0 python 与 python 字典

[英]KeyError: 0 python with python dict

So I'm trying to re-create a spaceInvaders game with python.所以我正在尝试用 python 重新创建一个 spaceInvaders 游戏。 Here is where my aliens are stored:这里是存放我的外星人的地方:

aliens = dict()

and each time we increase the game level then a new alien is added with it's id as a key and his positions and health(each alien has 2 health and dies after two shots which deal one damage each) as his value, so the aliens dictionary can look like that:每次我们提高游戏级别时,都会添加一个新的外星人,它的 id 作为键,他的位置和生命值(每个外星人有 2 生命值,在两次射击后死亡,每次造成一次伤害)作为他的值,所以外星人字典看起来像这样:

aliens = {
0: [(100, 50), 2],
1: [(50, 200), 1],
etc...
}

now each time an alien dies I set it's health to -1 and set his positions to (-100, -100) to put him out of the view, now the problem occurs when I call a cleanAlien() function which iterates through the alien array and pops every alien that has a health equal to -1 or an out of bounds position, here is that function:现在每次外星人死亡时,我将其生命值设置为-1并将他的位置设置为(-100, -100)以将他排除在视野之外,现在当我调用cleanAlien() function 迭代外星人时,就会出现问题数组并弹出所有生命值等于-1或超出范围 position 的外星人,这是 function:

def cleanAliens():
    global aliens
    for i in range(len(aliens)):
        if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
            aliens.pop(i);
            i -= i

However when I try to run that I get an error:但是,当我尝试运行时,出现错误:

if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
KeyError: 0

Any ideas?有任何想法吗?

Thanks in advance.提前致谢。

I'm not sure for the reason of this specific error, but it seems like the structure of aliens is not as expected.我不确定这个特定错误的原因,但似乎aliens的结构不像预期的那样。

However, you can iterate over the keys of the dictionary instead of over the size of it like so:但是,您可以遍历字典的键,而不是像这样遍历它的大小:

for i in list(aliens.keys()):
    if aliens[i][0] == (-100, -100) or aliens[i][1] < 0:
        aliens.pop(i)

And that way you don't get into this key trouble.这样你就不会陷入这个关键问题。

The list(aliens.keys()) returns a copy of the keys in the dictionary so you iterate over it, and after you remove one from the dictionary, you just continue to the next one. list(aliens.keys())返回字典中键的副本,以便您对其进行迭代,从字典中删除一个后,您只需继续下一个。

(btw, the i -= i line is meaningless because i will be set next iteration according to its iterator, so you can't actually change its value) (顺便说一句, i -= i行没有意义,因为i将根据其迭代器设置下一次迭代,因此您实际上无法更改其值)

Instead of:代替:

for i in range(len(aliens)):
    if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
        aliens.pop(i);
        i -= i

you should have used a comprehension:你应该使用理解:

aliens = { k:v for k,v in aliens.items() if v[0] != (-100,100) and v[1] >= 0 }

UPDATE: testing after receiving the comment about "the same error":更新:收到有关“相同错误”的评论后进行测试:

>>> aliens = {
... 0: [(100, 50), 2],
... 1: [(50, 200), 1],
... }
>>> aliens = { k:v for k,v in aliens.items() if v[0] != (-100,100) and v[1] >= 0 }
>>> 

There's no error.没有错误。

EDIT: Fixed my error by creating a list which is appended each key to be removed which is then passed to this function:编辑:通过创建一个列表来修复我的错误,该列表附加了每个要删除的键,然后传递给这个 function:

def Aliens():
    global aliens
    clean = list()
    for i in aliens.keys():
        if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
             clean.append(i)
    cleanAliens(clean)

and

def cleanAliens(cleanList):
    global aliens
    for i in range(len(cleanList)):
            aliens.pop(cleanList[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM