AI Python - TypeError:'int'对象没有属性'__getitem__'

时间:2018-02-16 20:10:30

标签: python

在我正在处理的AI代码中,程序必须检查棋盘上的方块是否为空。这由零的nxn矩阵表示,在这部分代码中,程序检查当前方块(数组中的坐标)是否为空:

# Check if a square is empty
def square_is_empty(i,j, state):
      if state[i][j] == 0:
         return True
      return False   

但是,我收到输出错误说:

in square_is_empty
    if state[i][j] == 0:
TypeError: 'int' object has no attribute '__getitem__'

尽管已经阅读了关于同一问题的多个答案,但我还是没有设法解决我的问题! 谢谢

1 个答案:

答案 0 :(得分:0)

以某种方式,您传递的state数组本身就是int,并在访问[i]元素或state数组int时触发此错误在其中,它在[j]元素访问:

上触发此错误

您可能应该查看state变量的方式,并确定它可能包含int而不是列表数据类型的原因。最简单的方法是打印print state