[Tutor] The Game of Life question
Kooser, Ara S
askoose at sandia.gov
Wed Jan 5 16:15:52 CET 2005
This is most likely a silly question and me not understanding python
enough. I am a mentor for some high school kids participating in a
supercomputing challenge. My background in programming is F77 (yeah
laugh it up) and I want the kids to learn python and use it for the
challenge.
They picked a project to model the flow of smallpox in a city and
surroundings areas. So I saw the game of life and thought maybe they
could modify it for use as a smallpox model. My question is when I run
this code as is and execute the command to generate a world, I get the
following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
print_world(make_random_world(10, 10))
File "C:\Python23\gameoflife.py", line 12, in make_random_world
world[i, j] = random.choice([LIVE, DEAD])
NameError: global name 'random' is not defined
>>>
Does "random" need to be defined after LIVE,DEAD or am I just missing
something. I was trying to run this on my work computer which is a winXP
machine running python 2.4.
Thanks,
Ara
This is the part of the game of life program I am trying to get to run
LIVE, DEAD = '*', '.'
def make_random_world(M, N):
"""Constructs a new random game world of size MxN."""
world = {}
for j in range(N):
for i in range(M):
world[i, j] = random.choice([LIVE, DEAD])
world['dimensions'] = (M, N)
return world
def print_world(world):
"""Prints out a string representation of a world."""
M, N = world['dimensions']
for j in range(N):
for i in range(M):
print world[i, j],
print
>>> print_world(make_random_world(10, 10))
More information about the Tutor
mailing list