[Tutor] designing POOP

Tiger12506 keridee at jayco.net
Fri Feb 8 03:40:42 CET 2008


There's a couple of errors in here that no one has addressed yet because the 
question was geared towards programming style... So now I will address them. 
Or undress them, I suppose. ;-)

> #!/user/bin/python
> """
>>From the testing laboratory of:
> b h a a l u u at g m a i l dot c o m
> 2008-02-07
> """
>
> import time
>
> CS = "\n"*50

This is okay, but it is a little more intuitive to make this
def clrscr():
  print "\n"*50

so that you can say
clrscr()

because clearing the screen is an action, not a thing that you would 
normally think of printing. The point of python is to make programming 
easier for humans. You defeat it's greatest power by not using it in a style 
that takes advantage of it's ability to mimic the real world. actions 
usually indicate functions.  (This is definitely a style thing. I'm not 
trying to start an argument)

> class TestClass1(object):
>    """ please give me a better name"""
>    def __init__(self):
>        """please document me"""
>        self.name = ""
>        self.answer = ""
>        self.strength = 20
>        self.wealth = 45
>        self.light = 0
>        self.tally = 0

As mentioned before, data in class should be related to one real (or 
abstract) object, not just a container for random data.

> def main():
>    tc1 = TestClass1() # instance
>    tc1.__init__() # invoke method
>    print CS
>    N1 = tc1.name
>    N1 = raw_input(" WHAT IS YOUR NAME, EXPLORER? ")

This is pointless. When you assign the value of tc1.name (which is "") you 
immediately overwrite it when you say N1 = raw_input(...) What you are 
trying to accomplish without grokking assignment is this.

tc1.name = raw_input("What is your name, explorer?")

>    # main game loop
>    while True:
>        print CS
>        print (" %s, YOUR STRENGTH IS %d") % (N1, tc1.strength)

Earlier you set N1, but not tc1.name. Either use one or the other.

>        print (" YOU HAVE $%d") % tc1.wealth
>        if tc1.light == 0:

Can be
if tc1.light:
  print("The light's on, but no one's home")
else:
  print("It is too dark to see anything")

>            print (" IT IS TOO DARK TO SEE ANYTHING")
>        else:
>            print (" THE LIGHT'S ON, BUT NO ONE'S HOME")
>        print
>        print
>        A = tc1.answer

Again, the same problem with tc1.name. Why are you bothering with A? Please 
realize that you can assign directly

tc1.answer = raw_input(...)

and just use the one variable. This is not C, but Python.

>        A = raw_input(" WHAT DO YOU WANT TO DO? [Q|L]: ") # main game 
> prompt
>        if A.upper() == "Q":
>            break
>        if A.upper() == "L":
>            light = raw_input(" LIGHT? [0|1]: ") # turn the light on and 
> off
>            if light == 0 and tc1.light == 0:

What?!? What is the deal? Do you have two different lights? No... then using 
two different light variables does not make sense. Also, the variable 
'light' will never be == 0 unless you hit enter without entering anything 
because raw_input returns a string. "0" != 0

>                print (" THE LIGHT IS OFF")

I see. You have two seperate variables so that you can determine whether the 
light was off previously. In which case, I suggest you change the variable 
to a more intuitive name, such as 'chosen' or something. To test whether the 
light was on before or not, you should have a method of the class 
islighton() because you are performing an action. Checking to see whether 
the light is on. If this doesn't make sense to you, consider that often 
attributes of a class are not directly accessed. There are even special 
things called properties that define get and set methods of classes... Um, 
disregard that. Too much info. :-)

>                time.sleep(2)
>            if tc1.wealth <= 0:

You have put this wealth check after the changing of the light. So it is 
possible that someone can change the light after they already have no money.

>                print
>                print (" YOU HAVE NO MONEY")
>                time.sleep(2)
>            else:
>                tc1.light = int(light)

Good. You just didn't think of int() in the if check. btw, you aren't going 
to tell anyone when they turn the light on?

>                tc1.wealth -= 15
>        else:
>            print (" INVALID CHOICE")
>            time.sleep(2)
>        tc1.tally += 1
>        tc1.strength -= 5
>        if tc1.strength <= 0:
>            print (" YOU DIED....")
>            time.sleep(2)
>            break
>    print
>    print (" Final Score:")
>    print ("    Tally: %d") % tc1.tally
>    print ("   Wealth: $%d") % tc1.wealth
>    print (" Strength: %d") % tc1.strength
>
> if __name__ == "__main__":
>    main()



More information about the Tutor mailing list