-
Notifications
You must be signed in to change notification settings - Fork 15
/
lesson_5.2_example.py
47 lines (35 loc) · 979 Bytes
/
lesson_5.2_example.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
class Fish:
def __init__(self, name, color, species):
self.name = name
self.color = color
self.species = species
self.alive = True
def bye_bye(self):
self.alive = False
def __repr__(self):
if self.alive:
eye = "o"
else:
eye = "*"
return f"><((({eye}>"
class MegaFish(Fish):
def __init__(self, name, color, species):
super().__init__(name, color, species)
self.lives = 10
def bye_bye(self):
self.lives -= 1
if self.lives == 0:
self.alive = False
def eat(self, fish):
print(f"yum, I ate {fish.name}")
fish.bye_bye()
print(f"{fish.name} is {fish}")
if __name__ == "__main__":
pet = Fish("jimmy", "blue", "goldfish")
print(pet.name)
print(pet)
print()
mega_pet = MegaFish("REEE", "rainbow", "swordfish")
print(mega_pet.name)
print(mega_pet)
mega_pet.eat(pet)