Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
1,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2.  
  3.  
  4. class Animal(ABC):
  5.     def __init__(self, name, age):
  6.         self.name = name
  7.         self.age = age
  8.  
  9.     @abstractmethod
  10.     def make_sound(self):
  11.         pass
  12.  
  13.  
  14. animals = []
  15.  
  16. class Dog(Animal):
  17.     def __init__(self, name, age, number_of_legs ):
  18.         Animal.__init__(self, name, age)
  19.         self.number_of_legs = int(number_of_legs)
  20.  
  21.     def make_sound(self):
  22.         return "I'm a Distinguishedog, and I will now produce a distinguished sound! Bau Bau."
  23.  
  24.     def __str__(self):
  25.         return f'Dog: {self.name}, Age: {self.age}, Number Of Legs: {self.number_of_legs}'
  26.  
  27.  
  28. class Cat(Animal):
  29.     def __init__(self, name, age, intelligence_quotient ):
  30.         Animal.__init__(self, name, age)
  31.         self.intelligence_quotient = int(intelligence_quotient)
  32.  
  33.     def make_sound(self):
  34.         return "I'm an Aristocat, and I will now produce an aristocratic sound! Myau Myau."
  35.  
  36.     def __str__(self):
  37.         return f'Cat: {self.name}, Age: {self.age}, IQ: {self.intelligence_quotient}'
  38.  
  39.  
  40.  
  41. class Snake(Animal):
  42.     def __init__(self, name, age, cruelty_coefficient ):
  43.         Animal.__init__(self, name, age)
  44.         self.cruelty_coefficient  = int(cruelty_coefficient )
  45.  
  46.     def make_sound(self):
  47.         return "I'm a Sophistisnake, and I will now produce a sophisticated sound! Honey, I'm home."
  48.  
  49.     def __str__(self):
  50.         return f'Snake: {self.name}, Age: {self.age}, Cruelty: {self.cruelty_coefficient}'
  51.  
  52.  
  53. while True:
  54.     data = input()
  55.     cls = data.split()[0]
  56.  
  57.     if cls == "I'm":
  58.         break
  59.  
  60.     elif cls == 'talk':
  61.         name = data.split()[1]
  62.         obj = list(filter(lambda animal: animal.name == name, animals))[0]
  63.         print(obj.make_sound())
  64.  
  65.     else:
  66.         name = data.split()[1]
  67.         age = data.split()[2]
  68.         third_param = data.split()[3]
  69.  
  70.         string = f'{cls}("{name}", {age}, {third_param})'
  71.         obj = eval(string)
  72.         animals.append(obj)
  73.  
  74.  
  75. dogs = list(filter(lambda animal: isinstance(animal, Dog), animals))
  76. cats = list(filter(lambda animal: isinstance(animal, Cat), animals))
  77. snakes = list(filter(lambda animal: isinstance(animal, Snake), animals))
  78.  
  79. sorted_animals = dogs + cats + snakes
  80.  
  81. for animal in sorted_animals:
  82.     print(animal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement