简体   繁体   中英

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. 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:

# 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:

You should probably have a look at how your state variable and determine why it might contain int instead of list data types. The easiest way to do this would be by just printing it print state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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