题目:
Warrior 和 Knight,两者的生命值(50点)相同但攻击力不相同,Warrior 攻击力为 5 点,Knight 攻击力为 7 点,他们之间不断来回攻击,每一个回合,被攻击者将减去相应的生命值,战斗以其中之一的死亡而告终。
链接:
https://py.checkio.org/en/mission/the-warriors/
代码:
class Warrior:
health = 50
is_alive = True
attack = 5
class Knight(Warrior):
attack = 7
def fight(unit_1, unit_2):
while unit_1.health > 0:
unit_2.health -= unit_1.attack
unit_1.health -= unit_2.attack
if unit_2.health > unit_1.health:
unit_1.is_alive = False
return False
else:
unit_2.is_alive = False
return True