[Tutor] Question about classes

Alan Gauld alan.gauld at btinternet.com
Sat Aug 25 10:48:56 CEST 2007


"Ara Kooser" <ghashsnaga at gmail.com> wrote


>   I am working on trying to understand classes by creating a
> character generator for a rpg.

You are so ar off base at the moment that I suggest you
go back to basics and try a much simpler class.

Your MAIN is not really a class at all, it's a function.
Classes represent things(ie Objects). Main is not a thing.
Secondly classes should not contain any directly
executable code such as print/raw_input etc
(they can but it's bad practice) Instead, a class should just
be a collection of data attruibutes and methods.

In your case it would be more appropriate to create
a Character class with methods to do whatever characters
do...

> import random
>
> class Main:
>
>    print """Welcome to the Classic Traveller character generator.
> Written in Python"""
>    print """Press return to generate a character"""
>    raw_input()
>
>
>
>    def upp():

methods of classes should have a first parameter of 'self'

>        print """Generating your UPP."""
>        print
>
>        strength = 0
>        dexterity = 0
>        endurance = 0
>        intelligence = 0
>        education = 0
>        social = 0
>
>        strength = random.randrange(2,12)
>        dexterity = random.randrange(2,12)
>        endurance = random.randrange(2,12)
>        intelligence = random.randrange(2,12)
>        education = random.randrange(2,12)
>        social = random.randrange(2,12)

There is no point in setting them all to zero then initialising
them again via randrange. Just do it once.

>        return strength, dexterity, endurance, intelligence, 
> education, social

But in a class you wouldn't expect to return these values here
you would expect to store them as attributes of an object
and return the object.

>    print upp()

>    def reroll():
>
>        a = raw_input("Are you satisfied with your UPP? Choose yes or
> no.").lower()
>        try:
>
>            if a == "yes":
>                career()
>
>            elif a == "no":
>                upp()
>
>            else:
>                print "Please choose a valid option."
>                print
>                reroll()
>
>        except ValueError:
>            print "Please choose a valid option."
>            print
>            reroll()
>
>        return
>
>    print """You have a chance to reroll if needed."""
>    reroll()
>
>    def career():
>        print """You will now choose are career path for your 
> character."""

Basically you don;t really have a class here at all you have a
main function calling other functions but because you've
wrapped them in a class its confusing things.

Try simplifying your class to just define the attributes,
something like this:

class Upp:
    def __init__(self):
        print "Creating your Upp"
        self.strength = random.randrange(2,12)
        self.dexterity = random.randrange(2,12)
        self.endurance = random.randrange(2,12)
        self.intelligence = random.randrange(2,12)
        self.education = random.randrange(2,12)
        self.social = random.randrange(2,12)

   def display(self)

        print """
              strength = %s
              dexterity = %s
              endurance = %s
              intelligence = %s
              education = %s
              social = %s""" % (self.strength, 
self.dexterity,self.endurance,
                                         self. 
intelligence,self.education, self.social)

Now in your main function you can create Upps like so:

#create 3 Upps:
players = []
for p in range(3):
   players.append(Upp())

# show the players we created

for p in players:
    p.display()

If that is not clear then you need to go back to the tutorials
again and work through the basics of wqhat a class and
object are.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list