Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # animal program with subclasses  and main fun function
  2. # the init class has feed,get_name,is_hungry and  talk  function)
  3. #the main function feed create animals, and feed the animal and
  4. #the main function in out of the the main class
  5.  
  6. class Animal(object):
  7.     def __init__(self, name, hunger, animal):
  8.         self.name=name
  9.         self.hunger=hunger
  10.         self.animal=animal
  11.     def get_name(self):
  12.         return self.name
  13.    
  14.     def feed(self):
  15.         self.hunger=0
  16.    
  17.     def is_hungry(self):
  18.         if self.hunger>0:
  19.             return True
  20.         else:
  21.             return False
  22.    
  23.     def talk(self):
  24.         pass    
  25.  
  26. # subclasses of main class Animal
  27.  
  28. class Dog(Animal):
  29.     pass
  30. class Cat(Animal):
  31.     pass
  32. class Skunk(Animal):
  33.     pass
  34. class Unicorn(Animal):
  35.     pass
  36.              
  37. class Dragon(Animal):
  38.     pass
  39.    
  40. def main():
  41.    
  42.     Brownie=Dog("Brownie",10,"dog")
  43.     Zelda=Cat("Zelda",3, "cat")
  44.     Stinky=Skunk("Stinky",0, "skunk")
  45.     Keith=Unicorn("Keith",7,"unicorn")
  46.     Lizzy=Dragon("Lizzy",1450,"dragon")
  47.     zoo_lst=[]
  48.     zoo_lst.append(Brownie)
  49.     zoo_lst.append(Zelda)
  50.     zoo_lst.append(Stinky)
  51.     zoo_lst.append(Keith)
  52.     zoo_lst.append(Lizzy)
  53.    
  54.    
  55.     for item in zoo_lst:
  56.         print("Animal = %s, Hungry %s" %(item.name, item.is_hungry()))
  57.        
  58.        
  59.     for animal in zoo_lst:
  60.         if animal.is_hungry():
  61.             animal.feed()
  62.            
  63.            
  64.     for animal in zoo_lst:
  65.         print()
  66.         print("%s %s" %(animal.animal, animal.name))
  67.  
  68. #main function that create value inside  class(name,hunger)
  69. #the main function append  all details intoo list'feed the animals
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement