设置一个Cat类,name.hunt分别是名字和捕猎能力,如果值为0,则说明没有力量捕猎1.catch0函数,用于外出捕猎将会饿死 2.food0,用来控制hunt最大值是903.injured0,受伤一次,捕猎能力减少5,捕猎能力不能低于0设置一个对象是大橘猫,让它捕猎10次,受伤6次,每次都统计受伤后捕猎能力,
时间: 2024-03-11 21:47:01 浏览: 87
好的,以下是您所需的Cat类的代码:
```python
class Cat:
def __init__(self, name, hunt):
self.name = name
self.hunt = hunt
self.food = 0
self.injured = 0
def catch(self):
if self.hunt > 0:
self.food += 1
self.hunt -= 1
print(self.name + " caught a prey!")
else:
print(self.name + " is too weak to catch any prey and will starve to death.")
def injured(self):
if self.hunt >= 5:
self.hunt -= 5
self.injured += 1
print(self.name + " got injured and lost some hunting ability.")
else:
self.hunt = 0
self.injured += 1
print(self.name + " got badly injured and can no longer hunt.")
# 创建对象
dajumao = Cat("大橘猫", 90)
# 捕猎10次,受伤6次
for i in range(10):
dajumao.catch()
for i in range(6):
dajumao.injured()
print("After being injured " + str(dajumao.injured) + " times, " + dajumao.name + "'s hunting ability is now " + str(dajumao.hunt) + ".")
```
希望这可以帮到您!
阅读全文