'NoneType'对象不可迭代

时间:2015-11-07 11:35:09

标签: python typeerror nonetype

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

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()来获取其图像

  

当我打印PacMan.SPRITE_UP [0]时,它返回正确的   元组

1 个答案:

答案 0 :(得分:0)

检查a.symbol()返回的值。看起来它试图将它解开为两个值并失败。

执行此操作时:

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

它调用a.symbol(),它返回一个值。代码假设这一点 value是一个包含两个值的iterable。然后是xsys 改为引用这两个值。

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