Search code examples
pythontypeerror

TypeError: 'int' object is not subscriptable board[y][x]


TypeError: 'int' object is not subscriptable board[y][x]

Code:

    board = []
    bWidth = 10
    bHeight = 20
    for h in range(bHeight):
        row = []
        for w in range(bWidth):
            row.append(0)
        board.append(row)
    
    for y in range(bHeight):
            for x in range(bWidth):
                if board[y][x] == 0:

Error:

  File "C:\Users\TUF\PycharmProjects\Tetris\main.py", line 92, in drawWindow
if board[y][x] == 0:

TypeError: 'int' object is not subscriptable


Solution

  • it appears as though the code is missing something inside the second loop. If i put a generic print statement there, it does get to the end and produces an array of zero's.

    board = []
    bWidth = 10
    bHeight = 20
    for h in range(bHeight):
        row = []
        for w in range(bWidth):
            row.append(0)
        board.append(row)
    
    for y in range(bHeight):
            for x in range(bWidth):
                if board[y][x] == 0:
                    print('hello', x, y)  # <--- this or pass
    
    board
    

    gives this:

    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]