[Tutor] Class methods

David Merrick merrickdav at gmail.com
Wed Jun 22 07:14:05 CEST 2011


# Critter Caretaker
# A virtual pet to care for

class Critter(object):

    """A virtual pet"""
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom

    # __ denotes private method
    def __pass_time(self):
        self.hunger += 1
        self.boredom += 1
        self.__str__()

    def __str__(self):
        print("Hunger is",self.hunger, "Boredom is " ,self.boredom)
        print("Unhappines is ",self.hunger + self.boredom," and Mood is
",self.mood)



    @property
    def mood(self):
        unhappiness = self.hunger + self.boredom
        if unhappiness < 5:
            m = "happy"
        elif 5 <= unhappiness <= 10:
            m = "okay"
        elif 11 <= unhappiness <= 15:
            m = "frustrated"
        else:
            m = "mad"
        return m

    def talk(self):
        print("I'm", self.name, "and I feel", self.mood, "now.\n")
        self.__pass_time()


    def eat(self):
        food = int(input("Enter how much food you want to feed your critter:
"))
        print("Brruppp.  Thank you.")
        self.hunger -= food
        # hunger = 0 at iniatition
        # self.hunger = self.boredom - food
        if self.hunger < 0:
            self.hunger = 0
        self.__pass_time()


    def play(self):
        fun = int(input("Enter how much fun you want your critter to have:
"))
        print("Wheee!")
        self.boredom -= fun
        # boredom = 0 at iniatition
        # self.boredom = self.boredom - fun
        if self.boredom < 0:
            self.boredom = 0
        self.__pass_time()


class Farm(Critter):
    def __init__(self,farmlet):
       Critter.__init__(self,farmlet)
       self.farmlet = farmlet

    def talk(self,farmlet):
        for critter in farmlet:
            print("Hello")
            Critter.talk(farmlet)

def main():
    crit1 = Critter("Sweetie")
    crit2 = Critter("Dave")
    farmlet = [crit1,crit2]
    f = Farm(farmlet)

    choice = None
    while choice != "0":
        print \
        ("""
        Critter Caretaker

        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter
        """)

        choice = input("Choice: ")
        print()

        # exit
        if choice == "0":
            print("Good-bye.")

        # listen to your critter
        elif choice == "1":
            f.talk(farmlet)

        # feed your critter
        elif choice == "2":
            f.eat(farmlet)

        # play with your critter
        elif choice == "3":
            f.play(farmlet)

        # some unknown choice
        else:
            print("\nSorry, but", choice, "isn't a valid choice.")

main()
("\n\nPress the enter key to exit.")

*OUTPUT*

 Critter Caretaker

        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter

Choice: 1

Hello
Traceback (most recent call last):
  File "D:/David/Python/programs/critter_farm3.py", line 115, in <module>
    main()
  File "D:/David/Python/programs/critter_farm3.py", line 101, in main
    f.talk(farmlet)
  File "D:/David/Python/programs/critter_farm3.py", line 72, in talk
    Critter.talk(farmlet)
  File "D:/David/Python/programs/critter_farm3.py", line 38, in talk
    print("I'm", self.name, "and I feel", self.mood, "now.\n")
AttributeError: 'list' object has no attribute 'name'

I am trying to access Crit1 and Crit2 namr in farmlet
-- 
Dave Merrick

merrickdav at gmail.com

Ph   03 3423 121
Cell 027 3089 169
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110622/9a79bc05/attachment.html>


More information about the Tutor mailing list