-
Notifications
You must be signed in to change notification settings - Fork 0
/
_45Zoo.py
86 lines (71 loc) · 2.3 KB
/
_45Zoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Animal():
def __init__(self, color, number_of_legs):
self.species = self.__class__.__name__
self.color = color
self.number_of_legs = number_of_legs
def __repr__(self):
return f'{self.color} {self.species}, {self.number_of_legs} legs'
class Wolf(Animal):
def __init__(self, color):
super().__init__(color,4)
class Sheep(Animal):
def __init__(self, color):
super().__init__(color,4)
class Snake(Animal):
def __init__(self, color):
super().__init__(color,0)
class Parrot(Animal):
def __init__(self, color):
super().__init__(color,2)
class Cage():
def __init__(self, id_number):
self.id_number = id_number
self.animals = []
def add_animals(self, *animals):
for one_animal in animals:
self.animals.append(one_animal)
def __repr__(self):
output = f'Cage {self.id_number}\n'
output += '\n'.join('\t' + str(animal) for animal in self.animals)
return output
class Zoo():
def __init__(self):
self.cages = []
def add_cages(self, *cages):
for cage in cages:
self.cages.append(cage)
def __repr__(self):
return '\n'.join(str(one_cage) for one_cage in self.cages)
def animals_by_color(self, color):
return [one_animal
for one_cage in self.cages
for one_animal in one_cage.animals
if one_animal.color == color]
def animals_by_legs(self, number_of_legs):
return [one_animal
for one_cage in self.cages
for one_animal in one_cage.animals
if one_animal.number_of_legs == number_of_legs]
def number_of_legs(self):
return sum(one_animal.number_of_legs
for one_cage in self.cages
for one_animal in one_cage.animals)
if __name__ == '__main__':
wolf = Wolf('black')
sheep = Sheep('white')
snake = Snake('brown')
parrot = Parrot('green')
print(wolf)
print(sheep)
print(snake)
print(parrot)
c1 = Cage(1)
c1.add_animals(wolf, sheep)
c2 = Cage(2)
c2.add_animals(snake, parrot)
z = Zoo()
z.add_cages(c1, c2)
print(z)
print(z.animals_by_color('white'))
print(z.animals_by_legs(4))
print(z.number_of_legs())