【Python基础课-第27关上机作业】
富善崔轶
from random import randint, random
class Player:
def __init__(self, name):
self.name = name
self.health = 100
def attack(self, target):
print(self.name + '发起攻击')
damage = randint(10, 20)
# 为什么不用self.damage?
target.defend(damage)
# 为什么target可以直接调用defend(),不用self?
def defend(self, damage):
if random() > 0.2:
self.health -= damage
# 老问题,为什么不用self.damage?
print('%s受到%d点伤害,血量剩余%d' % (self.name, damage, self.health))
if self.health <= 0:
print(self.name + '被击败')
else:
print(self.name + '防御成功')
print('----------------------')
kakarotto = Player('卡卡罗特')
piccolo = Player('比克大魔王')
while True:
if kakarotto.health > 0:
kakarotto.attack(piccolo)
else:
break
if piccolo.health > 0:
piccolo.attack(kakarotto)
else:
break
你的回复
回复请先 登录 , 或 注册相关内容推荐
最新讨论 ( 更多 )
- 三天背完《Python背记手册》,永远的神! (豆包)
- Python从零基础到入门 (爱编程的老陈)
- 自用的一套Python零基础学习资料,有需要可以直接拿 (momo)
- 盘点Python入门必备神仙书记「附PDF」 (天南星)
- 有人愿意学嘛❗️有人愿意学嘛❗️ (斤斤子爱分享)