[Tutor] Minesweeper implementing 20%, weird problem with matrixes

Alan G alan.gauld at freenet.co.uk
Thu Jul 14 02:12:18 CEST 2005


> I initialize them by putting None along all the fields 

Actually you don't.

    def initialize(self):
        self.buttonmatrix=[]
        self.statematrix=[]
        self.bombmatrix=[]

3 empty lists, so far so good.

        for i in range(self.fil):
            aux=[]
            for j in range(self.col):
                aux.append(None)

a new list filled with None

            self.buttonmatrix.append(aux)
            self.bombmatrix.append(aux)
            self.statematrix.append(aux)

THat same list inserted into all three lists, so they all point 
at the same internal list of Nones. So when you change one, 
you change all.

Try using aux[:] instead to take a copy of aux...
You could also use list comprehension instead of the loop/append:

aux = [None for i in range(self.col)]


HTH,

Alan G.


More information about the Tutor mailing list