# [Tutor] Game of python, help please.

Alan Gauld alan.gauld at btinternet.com
Tue Apr 17 21:14:58 CEST 2012

```On 17/04/12 19:23, leo degon wrote:
> Ok so I've done a bit of work on the program and rewrote it. I tried to
> take everyones advice. I've used more functions, I've made it so that it
> is a list of lists each containing an integer instead of another list
> with a single entry.

It still looks too complicated to me.

> #set intital distribution
>
> if initial=='r':
>      for i in range(x):
>          for j in range(y):
>              space[i].__setitem__(j,random.randint(0,1))

What's with the __setitem__ stuff? You should never normally need to
call that directly. I would expect something like

for i in range(x):
for j in range(y):
space[i][j] = randint(0,1)

Or, using list comprehensions:

for i in range(x):
space[i] = [randint(0,1) for j in range(y)]

Or even:

space = [[randint(0,1) for j in range(y)] for i in range(x)]

Which, for x,y = 3,4 gives a structure like:

>>> space
[[1, 0, 0, 0], [1, 0, 1, 1], [0, 1, 1, 0]]
>>>

> elif initial=='c':
>      for i in range(x):
>          for j in range(y):
>              if (i+j)%2==0:
>                  space[i].__setitem__(j,1)

Again I'd just use

space[i][j] = 1

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

```