• IndexError doesn't appear

Question related to mission Moore Neighbourhood

 

Is it ok, if i don't get IndexError when indexes are -1?

def count_neighbours(grid, row, col): dirs = ((-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)) n = 0 for dir in dirs: try: n += grid[row+dir[0]][col+dir[1]] except IndexError: continue return n