[Tutor] An Introduction and a question (continuing)
Python
python at venix.com
Sat Jun 10 00:10:28 CEST 2006
On Fri, 2006-06-09 at 16:28 -0500, Michael Sullivan wrote:
> OK. I've got it working this far. Now I want the script to generate
> eight pieces, each with a random colour. Here's my current code:
>
> #!/usr/bin/env python
>
> import random
> import time
> import math
>
> class LinePuzzlePiece:
> """This class defines a single playing piece for LinePuzzle"""
> def __init__(self):
> random.seed(time)
The seed allows you to reuse a particular set of random numbers - which
can be highly useful in testing so that successive runs use the same
numbers. In general, there is no need to call seed.
You are calling seed with the same value every time, a reference to the
time module. You really wanted:
random.seed(time.time())
I'd recommend simply deleting the call to random.seed.
If you do find you want to re-use the same set of "random" numbers, call
seed once right after you import random with a known value. If your
computer really needs to call seed to randomize effectively, then you
can try seeding with time.time().
> index = int(math.floor(random.uniform(0, 8)))
> colorlist = ["red", "blue", "green", "yellow", "purple", "cyan",
> "orange", "white"]
> self.color = colorlist[index]
>
> def printcolor(self):
> print self.color
>
> piececount = 0
> mypiece = ["", "", "", "", "", "", "", "", ""]
> while (piececount < 9):
> mypiece[piececount] = LinePuzzlePiece()
> mypiece[piececount].printcolor()
> piececount += 1
A suggestion:
mypieces = [] # pluralize container names
piececount = 9
for x in range(piececount):
>
> The problem is that while eight pieces are created and assigned a
> colour, the colour is always the same. I need the colours of the pieces
> to be in a somewhat random order. What am I doing wrong?
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list