[Tutor] designing POOP

bhaaluu bhaaluu at gmail.com
Fri Feb 8 22:25:27 CET 2008


On Feb 8, 2008 3:24 PM, Kent Johnson <kent37 at tds.net> wrote:
> ....
> and change the loop from
>    while True:
> to
>    while explr.alive:
>
> This would give you an Explorer class that actually does something useful.
>
> Kent
>

It also cleaned up main(), and put everything in well defined packages
at the top of the program. I can see do difference in "game play". 8^D
Here are your changes implemented, and working on my Linux system:

#!/user/bin/python

import time

class Explorer(object):
    """player"""
    def __init__(self,name):
        """initilaization method"""
        self.name = name
        self.strength = 20
        self.wealth = 60
        self.alive = True

    def __str__(self):
        return " %s, YOUR STRENGTH IS %d\n YOU HAVE $%d" % (self.name,
self.strength, self.wealth)

    def change_wealth(self, incr):
        self.wealth += incr
        if self.wealth <= 0:
            print
            print (" YOU HAVE NO MONEY")
            time.sleep(1)

    def change_strength(self, incr):
        self.strength += incr
        if self.strength <= 0:
            print ("\n\n YOU DIED...")
            time.sleep(1)
            self.alive = False

class Light(object):
    """light switch"""

    def __init__(self,light):
        self.light = light

    def __str__(self):
        if self.light == 0:
            return " IT IS TOO DARK TO SEE ANYTHING"
        else:
            return " THE LIGHTS ARE ON, BUT NO ONE'S HOME"

def cs():
    print "\n"*50

def main():
    tally = 0
    switch = Light(0) #instance
    cs() # clear screen
    name = raw_input(" WHAT IS YOUR NAME, EXPLORER? ")
    explr = Explorer(name)
    while explr.alive:
        cs() # clear screen
        print explr
        print switch
        print
        print
        answer = raw_input(" WHAT DO YOU WANT TO DO? [Q|L]: ")
        if answer.upper() == "Q":
            break
        if answer.upper() == "L":
                if switch.light == 1:
                    switch.light = 0
                else:
                    switch.light = 1
                    explr.change_wealth(-15)
                    explr.change_strength(-5)
        else:
            print (" INVALID CHOICE")
        tally += 1
    print
    print (" FINAL SCORE:")
    print ("    TALLY: %d" % tally)
    print (" STRENGTH: %d" % explr.strength)
    print ("   WEALTH: $%d" % explr.wealth)

if __name__ == "__main__":
    main()

Thanks Kent!
I like these small incremental changes with explanations.
I especially like the way you took blocks of code from main()
and made methods out of them. The actual code itself,
hardly changed!

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance.  Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]


More information about the Tutor mailing list