[Tutor] Using class methods
David Merrick
merrickdav at gmail.com
Tue Jun 21 10:12:04 CEST 2011
I need help using Class methods in another class. I have a class called
Critter and I want to create two critters in a farm Class Farm and use
Class Critter's methods
# 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.")
--
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/20110621/fc5f7d97/attachment.html>
More information about the Tutor
mailing list