将非int对象置为" Int对​​象不可订阅"

时间:2017-05-04 21:49:41

标签: python pygame

我正在使用pygame制作蛇游戏,我有2个变量xy我想要定义。我有以下内容:

def drawSnake(snakeCoords):
    for coord in snakeCoords:
        x = coord["x"] * CellSize
        y = coord['x'] * CellSize
        snake_segmentRect = pygame.draw.rect(x, y, CellSize, CellSize)
    pygame.draw.rect(displaysurf, green, snake_segmentRect)
    snake_segmentInnerRect = pygame.Rect(x + 4, y + 4, CellSize - 8, CellSize - 8)
    pygame.draw.rect(displaysurf, green, snake_segmentInnerRect)

...并收到错误:

line 175, in drawSnake
x = coord['x'] * CellSize
TypeError: 'int' object is not subscriptable

请帮助,其他类似的问题根本没用。

1 个答案:

答案 0 :(得分:1)

看起来像进入该函数的snakeCoords的值是一个整数列表。

打印一份确认声明:

def drawSnake(snakeCoords):
    for coord in snakeCoords:
        print(type(coord))
        x = coord["x"] * CellSize
        y = coord['x'] * CellSize

您应该看到以下输出python2:

<class 'int'>

或者在python3中:

<object 'int'>

这会导致您的错误。