[Tutor] lists of lists: more Chutes & Ladders!

Keith Winston keithwins at gmail.com
Tue Dec 31 06:59:17 CET 2013


I resolved a problem I was having with lists, but I don't understand how! I
caught my code inadvertently resetting/zeroing two lists TWICE at the
invocation of the game method, and it was leading to all the (gamechutes &
gameladders) lists returned by that method being zeroed out except the
final time the method is called. That is: the game method below is iterated
iter times (this happens outside the method), and every time gamechutes and
gameladders (which should be lists of all the chutes and ladders landed on
during the game) were returned empty, except for the last time, in which
case they were correct. I can see that doing the multiple zeroing is
pointless, but I can't understand why it would have any effect on the
returned values. Note that self.reset() is called in __init__, so the lists
exist before this method is ever called, if I understand properly.

    def game(self, iter):
        """Single game"""

        self.gamechutes[:] = []   #when I take out these two slice
assignments,
        self.gameladders[:] = []   # then gamechutes & gameladders work
properly

        self.gamechutes = []  # these were actually in a call to
self.reset()
        self.gameladders = []

        #.... other stuff in reset()

        while self.position < 100:
            gamecandl = self.move()
            if gamecandl[0] != 0:
                self.gamechutes.append(gamecandl[0])
            if gamecandl[1] != 0:
                self.gameladders.append(gamecandl[1])
        return [iter, self.movecount, self.numchutes, self.numladders,
self.gamechutes,            self.gameladders]

I'm happy to share the rest of the code if you want it, though I'm pretty
sure the problem lies here. If it's not obvious, I'm setting myself up to
analyse chute & ladder frequency: how often, in a sequence of games, one
hits specific chutes & ladders, and related stats.

As always, any comments on style or substance are appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131231/dcbd0dc4/attachment.html>


More information about the Tutor mailing list