简体   繁体   English

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

[英]AI Python - TypeError: 'int' object has no attribute '__getitem__'

In the AI code I am working on, the program has to check if a square on a chessboard is empty. 在我正在使用的AI代码中,程序必须检查棋盘上的正方形是否为空。 This is represented by a nxn matrix of zeros, and in this part of the code the program checks if the current square (coordinate in the array) is empty: 这由一个零的nxn矩阵表示,并且在代码的这一部分中,程序检查当前平方(数组中的坐标)是否为空:

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

However, I get an output error saying: 但是,我得到一个输出错误,说:

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

Despite having read multiple answers about the same issue, I still haven't managed to fix mine! 尽管阅读了关于同一问题的多个答案,但我仍然没有设法解决我的问题! Thank you 谢谢

Somehow your passed state array either is a int itself and triggers this error on the access of the [i] element or the state array has int s in it and it triggers this error on the [j] element access: 以某种方式,您传递的state数组本身就是一个int并在[i]元素的访问中触发此错误,或者state数组中包含int ,并且在[j]元素的访问时触发此错误:

You should probably have a look at how your state variable and determine why it might contain int instead of list data types. 您可能应该看一下state变量的方式,并确定为什么它可能包含int而不是list数据类型。 The easiest way to do this would be by just printing it print state 最简单的方法是仅将其打印为print state

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

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