Advertisement
horozov86

Zoo - Lab: Classes and Objects, Python Fundamentals

Feb 24th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. class Zoo:
  2.     __animals = 0
  3.  
  4.     def __init__(self, name):
  5.         self.name = name
  6.         self.mammals = []
  7.         self.fishes = []
  8.         self.birds = []
  9.  
  10.     def add_animal(self, species, name):
  11.         if species == "mammal":
  12.             self.mammals.append(name)
  13.         elif species == "fish":
  14.             self.fishes.append(name)
  15.         elif species == "bird":
  16.             self.birds.append(name)
  17.         Zoo.__animals += 1
  18.  
  19.     def get_info(self, species):
  20.         result = ""
  21.         if species == "mammal":
  22.             result = f"Mammals in {self.name}: {', '.join(self.mammals)}"
  23.         elif species == "fishes":
  24.             result = f"Fished in {self.name}: {', '.join(self.fishes)}"
  25.         elif species == "birds":
  26.             result = f"Birds in {self.name}: {', '.join(self.birds)}"
  27.  
  28.         return f"{result}\nTotal animals: {len(self.mammals) + len(self.fishes) + len(self.birds)}"
  29.  
  30.  
  31. name = input()
  32. rows = int(input())
  33.  
  34. zoo = Zoo(name)
  35. for _ in range(rows):
  36.     species, name = input().split()
  37.     zoo.add_animal(species, name)
  38.  
  39. info = input()
  40. print(zoo.get_info(info))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement