I'm an idiot/blind and I can't find why I'm getting a list index error. Care to take a look at these 20 or so lines?
Posted
by
Meff
on Stack Overflow
See other posts from Stack Overflow
or by Meff
Published on 2010-12-21T05:03:32Z
Indexed on
2010/12/21
5:31 UTC
Read the original article
Hit count: 244
Basically it's supposed to take a set of coordinates and return a list of coordinates of it's neighbors. However, when it hits here:if result[i][0] < 0 or result[i][0] >= board.dimensions:
result.pop(i)
when i is 2, it gives me an out of index error. I can manage to have it print result[2][0] but at the if statement it throws the errors. I have no clue how this is happening and if anyone could shed any light on this problem I'd be forever in debt.
def neighborGen(row,col,board):
"""
returns lists of coords of neighbors, in order of up, down, left, right
"""
result = []
result.append([row-1 , col])
result.append([row+1 , col])
result.append([row , col-1])
result.append([row , col+1])
#prune off invalid neighbors (such as (0,-1), etc etc)
for i in range(len(result)):
if result[i][0] < 0 or result[i][0] >= board.dimensions:
result.pop(i)
if result[i][1] < 0 or result[i][1] >= board.dimensions:
result.pop(i)
return result
© Stack Overflow or respective owner