On Aug 25, 2007, at 12:59 PM, Ara Kooser wrote:

> Hello all,
>
>    I am working on trying to understand classes by creating a
> character generator for a rpg. I know I am doing something silly but I
> am not sure what. When I run the program I and type no when prompted I
> get the following message:
> Traceback (most recent call last):
>   File "/Users/ara/Documents/ct_generator.py", line 10, in <module>
>     class Main:
>   File "/Users/ara/Documents/ct_generator.py", line 68, in Main
>     reroll()
>   File "/Users/ara/Documents/ct_generator.py", line 53, in reroll
>     upp()
> NameError: global name 'upp' is not defined
>
> I guess it doesn't recognize that I want to call the function upp()
> again. I think I might be using the wrong syntax here. My code is
> below. Thank you any help or guidance.

This is exactly the level I'm at right now, so I got all excited and  
tried to solve this properly. Unfortunately I've hit a new snag,  
which I hope the OP or someone else can help me solve. At least the  
below should represent progress.

Instead of calling general methods inside your class, you'll want to  
use the __init__ method, and then call the other methods from inside  
that, so that when a new instance of the class is created, all the  
methods act on that one new instance. That's what the first response  
was saying. So under __init__ we create a dictionary of attributes,  
and run both __upp and __career, all on the one new object. Showstats  
might as well be a separate function, because you'll want that  
available to players at any time.

The new problem is the while loop inside __upp. Every time I say  
"no", and it generates a new set of attributes, it seems to add  
another "layer" of unfinished = True, so that once I've got  
attributes I like, I need to say "yes" as many times as I said "no"  
before it will let me break out of the loop and go on my way. I guess  
this is because it's calling the entire method over again, but I  
don't really understand this, or how else to get around it. Any  
solutions (and, obviously, general corrections of the whole thing)  
would be appreciated.

Yrs,
Eric

==================

import random

class Character(object):
     """Welcome to the Classic Traveller character generator.
     Written in Python
     """
     def __init__(self):
         print "Generating statistics for your new character..."
         self.atts = {}
         self.__upp()
         self.__career()

     def __upp(self):
         for att in ('strength', 'dexterity', 'endurance',  
'intelligence', 'education', 'social'):
             self.atts[att] = random.randrange(2,12)
         self.showstats()
         unhappy = True
         while unhappy:
             a = raw_input("Are you satisfied with your UPP? Choose  
yes or no.").lower()
             if a == "yes":
                 unhappy = False
             elif a == "no":
                 print "Rerolling attributes..."
                 self.__upp()
             else:
                 print "Please choose a valid option."
                 continue
         return

     def __career(self):
         print "This is where the next step would go"

     def showstats(self):
         print "Your character's attributes are:"
         for attribute, value in self.atts.items():
             print "%s: %d" % (attribute.title(), value)

me = Character() 
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to