# Monster Battle -- finished version. # Two monsters trade blows until one faints. # Run with: python projects/monster-battle/finished.py import random class Monster: def __init__(self, name, hp, attack): self.name = name self.hp = hp self.attack = attack def is_alive(self): return self.hp > 0 def hit(self, other): damage = random.randint(1, self.attack) other.hp = other.hp - damage if other.hp < 0: other.hp = 0 print(f"{self.name} hits {other.name} for {damage}. {other.name} has {other.hp} HP left.") hero = Monster("Hero", 30, 8) goblin = Monster("Goblin", 25, 6) round = 1 while hero.is_alive() and goblin.is_alive(): print(f"-- Round {round} --") hero.hit(goblin) if goblin.is_alive(): goblin.hit(hero) round += 1 if hero.is_alive(): print(hero.name + " wins!") else: print(goblin.name + " wins!")