0

Returning an index out of range error, someone please help!

def floodfill(maze, x, y, a, b):
    #maze[x][y] = b
    for i in range(1,len(maze)):
        for j in range(len(maze[i])):
            if maze[i-1][j] == a or [i][j-1] == a or maze[i-1][j] == b or maze[i][j-1] == b:
                if maze[i][j] == a:
                    maze[i][j] = b
    return maze

here is the "maze" being solved.... the flood fill is supposed to change all 0 into 1

[[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], 
 [0, 0, 0, 0, 0, 0, -1, 0, -1, 0, -1], 
 [-1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1], 
 [-1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1], 
 [-1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1], 
 [-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1], 
 [-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1], 
 [-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1], 
 [-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1], 
 [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]]
hey
  • 23
  • 4

1 Answers1

2

In the 5th line of your code, in the first or, it just says [i][j-1] by itself.

>>> i = 9
>>> j = 10
>>> [i][j-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> 

You are trying to get the j-1th value of the integer i, which clearly does not work :).

Just put maze before these two to make this: maze[i][j-1].

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76