[Tutor] Question about classes

Kent Johnson kent37 at tds.net
Sat Aug 25 15:19:00 CEST 2007


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. 

This is a procedural program wrapped in a class declaration. Just get 
rid of "class Main:" and outdent everything and it will work better. It 
is rare to have code other than function definitions (methods) and 
simple assignment (class attributes) in the body of a class.

You might want to gather all the top-level code into a main() function 
and call that once, rather than intermingling the top-level code with 
the function definitions.

You need to do a bit more reading about classes in Python, you clearly 
misunderstand. I don't have time to explain now but try the beginner 
tutorials.

Kent

> 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.
> 
> Ara
> 
> ###################################################################################
> #Version: not even 0.1
> #By: Ara Kooser
> #
> ####################################################################################
> 
> 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():
>         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)
> 
>         return strength, dexterity, endurance, intelligence, education, social
> 
> 
>     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."""
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 



More information about the Tutor mailing list