Reputation: 59
Here's my code:
class Grid:
def __init__(self):
self.matrix = [[Cell(i, j)] for j in range(5) for i in range(6)]
# Extra stuff
def enter_letter(self, letter):
if self.current_col == 5:
return
print(self.matrix[0][1].get_letter()) # Test, this is where it goes wrong.
# It also goes wrong when I call set_letter() a 2nd time as it goes to matrix[0][1] in the line below:
self.matrix[self.current_row][self.current_col].set_letter(letter)
self.current_col += 1
# Extra stuff
class Cell:
def __init__(self, row, col):
self.row = row
self.col = col
# Extra stuff
And the thing is, every time I try to access or call set_letter()
on self.matrix[0][1]
, it gives me the IndexError: list index out of range. It's completely fine when I try to access matrix[4][0] but anything above a 0 for the y value, throws me an index error. I tried Googling and a lot of the suggestions was to call .append()
to each list to make it a 2D list (not sure if I explained that right) so I tried but I found it difficult to apply to a list of objects...
If I could get any suggestions at all, that would be great :)
Upvotes: 0
Views: 79
Reputation: 59
As TheLazyScripter suggested, here's the code I changed to make it work:
class Grid:
def __init__(self):
self.matrix = [[Cell(i, j) for i in range(6)] for j in range(5)]
Upvotes: 1