[Tutor] List of lists help

btkuhn at email.unc.edu btkuhn at email.unc.edu
Thu Nov 20 09:01:04 CET 2008


Hello,

I am completely baffled by this action and would appreciate any help. 
My problem is occurring within a class which is within a larger 
program; if I need to post the entire program let me know and I will. 
It's only about 250 lines so far. Anyways, I am using a list of lists 
to store data in a GUI game called goMoku. It is kind of like a connect 
five game, pretty simple. When a user clicks a certain square, this 
line is called to store the "move":

self.boardarray[row][col] = self.currentPlayer

self.currentPlayer is either "White" or "Black" which are constants set 
to 1 and 2, so that when, say, the black player clicks on row 2 column 
4, self.boardarray[2][4] will be set to "2". Instead, the program is 
setting multiple values within the list as 2. Here is an example 
output, when I click on (0,0):

[[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], etc.

The board is 13X13 and position 0 for each list in the list of lists is 
being set to 2, while only position 0 in the first list should be 
changed to 2. To check for errors, I've surrounded the statement by 
print statements to see what's going on, like this:

print self.boardarray
print row,col,self.currentPlayer,self.boardarray[row][col]
self.boardarray[row][col] = self.currentPlayer
print self.boardarray

My output is:

[[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], etc.
0 0 2 0
[[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], etc.

I do not understand what is going on. Everything is as expected except 
that the extra positions are being assigned as "2". Can anyone suggest 
what is going wrong?

Thanks,
Ben


More information about the Tutor mailing list