Search code examples
pythonpython-3.x

How to resolve the error "typeError 'int' object is not subscriptable"


I'm making a Connect Four game for a class and I'm running into an error with my play function.

def play(grid,column,checker):
    
    counter = 0
    for x in grid[0]:
        counter += 1
    print(counter)
    if counter > column-1 :
        for row in reversed(grid):
            if row[column-1] == "empty":
                row[column-1] = checker
                print(True)
                return True,grid,checker
    else:
        print(False)
        return False  , grid , checker

The problem occurs at line 9 (if row[column-1] == "empty") and I keep getting

typeError 'int' object is not subscriptable

grid is a global variable returned from a different function.


Solution

  • The problem here is actually in the different function grid is returned from. You must have made some mistake there, which causes that different function return something of the form [1, 6, 3, 8, 3], whereas your play function assumes something in the form of [[1, 5, 6, 2, 10], [1, 5, 6, 2, 10], [1, 5, 6, 2, 10], [1, 5, 6, 2, 10]].