上述问题输入输出为>>> vanilla = Cat('Vanilla', 'Minazuki Kashou') >>> isinstance(vanilla, Pet) # check if vanilla is an instance of Pet. True >>> vanilla.talk() Vanilla says meow! >>> vanilla.eat('fish') Vanilla ate a fish! >>> vanilla.lose_life() >>> vanilla.lives 8 >>> vanilla.is_alive True >>> for i in range(8): ... vanilla.lose_life() >>> vanilla.lives 0 >>> vanilla.is_alive False >>> vanilla.lose_life() Vanilla has no more lives to lose.
时间: 2024-03-03 18:51:04 浏览: 208
Sure, here is the modified implementation of the Cat class that meets your requirements:
```
class Cat(Pet):
def __init__(self, name, owner, lives=9):
super().__init__(name, owner)
self.lives = lives
def talk(self):
print(self.name + " says meow!")
def eat(self, thing):
super().eat(thing)
def lose_life(self):
if self.lives > 0:
self.lives -= 1
if self.lives == 0:
self.is_alive = False
else:
print(self.name + " has no more lives to lose.")
```
Here are the doctests:
```
>>> vanilla = Cat('Vanilla', 'Minazuki Kashou')
>>> isinstance(vanilla, Pet)
True
>>> vanilla.talk()
Vanilla says meow!
>>> vanilla.eat('fish')
Vanilla ate a fish!
>>> vanilla.lose_life()
>>> vanilla.lives
8
>>> vanilla.is_alive
True
>>> for i in range(8):
... vanilla.lose_life()
>>> vanilla.lives
0
>>> vanilla.is_alive
False
>>> vanilla.lose_life()
Vanilla has no more lives to lose.
```
I hope this helps!
阅读全文