More cats! Fill in this implemention of a class called NoisyCat, which is just like a normal Cat. However, NoisyCat talks a lot -- twice as much as a regular Cat! We may change the implementation of Pet and Cat while testing your code, so make sure you use inheritance correctly.其中class Pet: def init(self, name, owner): self.is_alive = True self.name = name self.owner = owner def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") def talk(self): print(self.name) 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.")
时间: 2024-03-03 10:51:14 浏览: 135
下面是实现 NoisyCat 类的代码:
```
class NoisyCat(Cat):
def talk(self):
print((self.name + " says meow! ") * 2)
```
NoisyCat 类继承自 Cat 类,并重写了 talk() 方法。在 NoisyCat 中,talk() 方法会调用 Cat 中的 talk() 方法,并将其输出内容重复一遍。这样,NoisyCat 对象的 talk() 方法输出的内容就是 Cat 对象的两倍。
相关问题
## Problem 3: Noisy Cat More cats! Fill in this implemention of a class called `NoisyCat`, which is just like a normal `Cat`. However, `NoisyCat` talks a lot -- twice as much as a regular `Cat`! We may change the implementation of `Pet` and `Cat` while testing your code, so make sure you use inheritance correctly. ```python class NoisyCat: # Dose this line need to change? """A Cat that repeats things twice. >>> chocola = NoisyCat('Chocola', 'Minazuki Kashou') >>> isinstance(chocola, Cat) # check if chocola is an instance of Cat. True >>> chocola.talk() Chocola says meow! Chocola says meow! """ def __init__(self, name, owner, lives=9): # Is this method necessary? If not, feel free to remove it. "*** YOUR CODE HERE ***" def talk(self): """Talks twice as much as a regular cat. """ "*** YOUR CODE HERE ***" ``` ##
To create a `NoisyCat` class that inherits from `Cat` and talks twice as much as a regular `Cat`, we can simply override the `talk` method in `Cat` as follows:
```python
class NoisyCat(Cat):
def talk(self):
"""Talks twice as much as a regular cat.
"""
super().talk()
super().talk()
```
Here, we first call the `talk` method of the parent class `Cat` using `super().talk()`, and then call it again to make the `NoisyCat` talk twice as much as a regular `Cat`.
We can then test the implementation as follows:
```python
>>> chocola = NoisyCat('Chocola', 'Minazuki Kashou')
>>> isinstance(chocola, Cat)
True
>>> chocola.talk()
Chocola says meow!
Chocola says meow!
```
Implement fastest_words, which returns which words each player typed fastest. This function is called once both players have finished typing. It takes in a game. The game argument is a game data abstraction, like the one returned in Problem 9. You can access words in the game with selectors word_at, which takes in a game and the word_index (an integer). You can access the time it took any player to type any word using the time function provided in cats.py. The fastest_words function returns a list of lists of words, one list for each player, and within each list the words they typed the fastest. In the case of a tie, consider the earliest player in the list (the smallest player index) to be the one who typed it the fastest. Be sure to use the accessor functions for the game data abstraction, rather than assuming a particular data format.
Here's a possible implementation of the `fastest_words` function:
```python
from typing import List
from game import Game
from cats import time
def fastest_words(game: Game) -> List[List[str]]:
num_players = len(game.times)
num_words = len(game.words)
fastest = [[] for _ in range(num_players)]
for w in range(num_words):
fastest_time = float('inf')
fastest_players = []
for p in range(num_players):
t = time(game, p, w)
if t < fastest_time:
fastest_time = t
fastest_players = [p]
elif t == fastest_time:
fastest_players.append(p)
for p in fastest_players:
fastest[p].append(game.word_at(w))
return fastest
```
This function takes in a `Game` object as input, and returns a list of lists of words indicating which words each player typed fastest. The `Game` object has two attributes: `words` and `times`. `words` is a list of strings representing the words in the game. `times` is a list of lists representing the time it took each player to type each word.
To compute the fastest words, we iterate over each word in the game. For each word, we determine the fastest time among all players, as well as which players achieved that time. We then append the word to the list of fastest words for each of those players.
Note that in the case of a tie, we consider the earliest player in the list to be the one who typed it the fastest. This is done by always appending the word to the list of the first player in the `fastest_players` list.
Finally, we return the `fastest` list, which contains one list of words for each player indicating which words they typed fastest.
阅读全文