'NoneType' 对象不可迭代

'NoneType' object is not iterable

我正在尝试迭代一个元组数组(以常量形式):

SPRITE_RIGHT = [(0, 0), (16, 0), (32, 0)]
SPRITE_LEFT = [(0, 16), (16, 16), (32, 0)]
SPRITE_UP = [(0, 32), (16, 32), (32, 0)]
SPRITE_DOWN = [(0, 48), (16, 48), (32, 0)]
def symbol(self):
    self._status += 1

    if (self._status > 2):
        self._status = 0

    if (self._dx > 0):
        (x, y) = PacMan.SPRITE_RIGHT[self._status]
        return (x,y)
    if (self._dx < 0):
        (x, y) = PacMan.SPRITE_LEFT[self._status]
        return (x,y)
    if (self._dy > 0):
        (x, y) = PacMan.SPRITE_DOWN[self._status]
        return (x,y)
    if (self._dy < 0):
        (x, y) = PacMan.SPRITE_UP[self._status]
        return (x,y)
...
for a in arena.actors():
        if not isinstance(a, Wall):
            x, y, w, h = a.rect()
            xs, ys = a.symbol()                #This line gives me the problem
            screen.blit(sprites, (x, y), area=(xs, ys, w, h))

当我执行程序时收到此错误:

TypeError: 'NoneType' object is not iterable

对于每个演员,我都调用方法 symbol() 来获取其图像

When i print PacMan.SPRITE_UP[0] for example it returns the correct tuple

检查 a.symbol() 返回的值。看起来它正试图将其展开为两个值但失败了。

当你这样做时:

xs, ys = a.symbol()     #This line gives me the problem

它调用 a.symbol(),其中 returns 是一个值。该代码假定这 value 是一个包含两个值的可迭代对象。然后 xsys 更改为对这两个值的引用。

如果 a.symbol() 返回的值不是这样的 可迭代,分配将失败。你得到的错误信息, TypeError: 'NoneType' object is not iterable,表明 a.symbol() 返回 None