[Tutor] Tutor Digest, Vol 88, Issue 86

David Merrick merrickdav at gmail.com
Tue Jun 21 22:44:47 CEST 2011


I have created a Critter object and a Farm object which is two critters. I
want to use Critters methods in the Farm object as it just 2 critters in a
list

crit1 = Critter("Sweetie")
crit2 = Critter("Dave")
farmlet = [crit1,crit2]

# 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,farm):
>         self.hunger += 1
>         self.boredom += 1
>         self.__str__()
>
>     def __str__(self,farmlet):
>         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,hunger = 0,boredom = 0):
> #A collection of Critters
>
>     def __init__(farmlet, name):
>         for critter in farmlet:
>             critter.name = name
>             critter.hunger = hunger
>             critter.boredom = boredom
>
>     def talk(self,farmlet):
>           for critter in farmlet:
>                 print(self,farmlet)
>
>     def __str__(farmlet):
>         for critter in farmlet:
>           print("Hunger is",critter.hunger, "Boredom is " ,critter.boredom)
>           print("Unhappines is ",critter.hunger + critter.boredom," and
> Mood is ",critter.mood)
>
>     def eat(farmlet):
>         for critter in farmlet:
>             food = int(input("Enter how much food you want to feed your
> critter: "))
>             print("Brruppp.  Thank you.")
>             farmlet.hunger -= food
>             # hunger = 0 at iniatition
>             # self.hunger = self.boredom - food
>             if farmlet.hunger < 0:
>                 farmlet.hunger = 0
>             farmlet.__pass_time()
>
> def main():
> ##    crit_name = input("What do you want to name your critter?: ")
> ##    crit = Critter(crit_name)
>     crit1 = Critter("Sweetie")
>     crit2 = Critter("Dave")
>     farmlet = [crit1,crit2]
>     farmlet.hunger = 0
>     farmlet.boredom = 0
>     farm = Farm.__str__(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":
>             farm = Farm.__str__(farmlet)
>
>         # feed your critter
>         elif choice == "2":
>             farm = Farm.eat(farmlet)
>
>         # play with your critter
>         elif choice == "3":
>             farm.play(farmlet)
>
>         # some unknown choice
>         else:
>             print("\nSorry, but", choice, "isn't a valid choice.")
>
>
>
>
>
> main()
> ("\n\nPress the enter key to exit.")
>



>
>
> Well it depends what you mean. If you have a critter object and want
> to invoke its method inside the farm, do somethinh like this.
> class Farm_class(object):
>  def feed(self, critter):
>    critter.eat(self.food)
>
> or if you want to use a method of the Critter class within the farm do
>
> class Farm_class(object):
>  def count(self):
>    print Critter_class.count()
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110622/2a72131e/attachment-0001.html>


More information about the Tutor mailing list