[Tutor] lists of lists: more Chutes & Ladders!
Mark Lawrence
breamoreboy at yahoo.co.uk
Tue Dec 31 09:11:06 CET 2013
On 31/12/2013 07:30, Keith Winston wrote:
> Never mind, I figured out that the slice assignment is emptying the
> previous lists, before the .reset() statements are creating new lists
> that I then populate and pass on. It makes sense.
>
> On Tue, Dec 31, 2013 at 12:59 AM, Keith Winston <keithwins at gmail.com
> <mailto:keithwins at gmail.com>> wrote:
>
> 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.
>
Please intersperse or bottom post, top posting makes things very
difficult to follow, especially in long threads.
TIA.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list