Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # animal program with subclasses and main fun function
- # the init class has feed,get_name,is_hungry and talk function)
- #the main function feed create animals, and feed the animal and
- #the main function in out of the the main class
- class Animal(object):
- def __init__(self, name, hunger, animal):
- self.name=name
- self.hunger=hunger
- self.animal=animal
- def get_name(self):
- return self.name
- def feed(self):
- self.hunger=0
- def is_hungry(self):
- if self.hunger>0:
- return True
- else:
- return False
- def talk(self):
- pass
- # subclasses of main class Animal
- class Dog(Animal):
- pass
- class Cat(Animal):
- pass
- class Skunk(Animal):
- pass
- class Unicorn(Animal):
- pass
- class Dragon(Animal):
- pass
- def main():
- Brownie=Dog("Brownie",10,"dog")
- Zelda=Cat("Zelda",3, "cat")
- Stinky=Skunk("Stinky",0, "skunk")
- Keith=Unicorn("Keith",7,"unicorn")
- Lizzy=Dragon("Lizzy",1450,"dragon")
- zoo_lst=[]
- zoo_lst.append(Brownie)
- zoo_lst.append(Zelda)
- zoo_lst.append(Stinky)
- zoo_lst.append(Keith)
- zoo_lst.append(Lizzy)
- for item in zoo_lst:
- print("Animal = %s, Hungry %s" %(item.name, item.is_hungry()))
- for animal in zoo_lst:
- if animal.is_hungry():
- animal.feed()
- for animal in zoo_lst:
- print()
- print("%s %s" %(animal.animal, animal.name))
- #main function that create value inside class(name,hunger)
- #the main function append all details intoo list'feed the animals
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement