[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: [Tutor] An Introduction and a question



On Fri, Jun 09, 2006 at 05:00:48PM -0300, Andre Roberge wrote:
> On 6/9/06, Michael Sullivan <michael@xxxxxxxxxxxxxxxx> wrote:
> >Here is my 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):
> >      seed(time)

The 'import' statement does not work this way.  You want to say

    random.seed(time.time())

Also note that to call a function you always have to add ().

Also, it is not necessary to manually set the random seed -- it is done
automatically.

> >      index = int(math.floor(uniform(1, 10)))       colorlist = ["red",
> >"blue", "green" "yellow", "purple"]       self.color = colorlist[index]

(Someone's mail client wrapped the lines unnecessarily.)

The random module has a very handy little function called 'choice':

        colorlist = ["red", "blue", "green", "yello", "purple"]
        self.color = random.choice(colorlist)

If you want a random integer from a certain range (1 <= x < 10), you can
say

        index = random.randrange(1, 10)

without having to go through the whole rigamarole of converting floating
point numbers to integers.  int() by the way floors the number (at least
if it is positive), so math.floor would be superfluous.

> >   def printcolor():
> >      print self.color

Others have already pointed out that printcolor should take one
argument, self.

> >mypiece = LinePuzzlePiece

You need to add () at the end to actually create an object of your
class.

> >mypiece.printcolor

And here, to call the function.  (I know that I'm repeating what other
have said, but I want to be complete.)


Here's the complete fixed program.  I've also fixed the indentation to
be 4 spaces, as is customary for Python:

#!/usr/bin/env python
import random

class LinePuzzlePiece:
    """This class defines a single playing piece for LinePuzzle"""

    def __init__(self):
        colorlist = ["red", "blue", "green", "yello", "purple"]
        self.color = random.choice(colorlist)

    def printcolor(self):
        print self.color

mypiece = LinePuzzlePiece()
mypiece.printcolor()

> 2. I'm cc-ing the pygame user group.  I suggest that future questions
> related to pygame be sent over to that group instead.  They are just
> as friendly as the folks on the tutor list.

/me waves in a friendly manner

Cheers!
Marius Gedminas
-- 
Microsoft does have a Year 2000 problem. We're it.

Attachment: signature.asc
Description: Digital signature