Function not looping

  Kiến thức lập trình

I have created a class with a method and imported it to the main script.

class Character:

    # INIT METHOD
    def __init__(self, name, char_class, hp, max_hp, damage) -> None:
        self.name = name
        self.char_class = char_class
        self.hp = hp
        self.max_hp = max_hp
        self.damage = damage

    # ATTACK METHOD
    def attack(self, target) -> None:
        target.hp -= self.damage
        target.hp = max(target.hp, 0)

        print(f"{self.name} dealt {self.damage} damage to {target.name}!")

On my main code, I created a fight() function to simulate a fight and constantly update each of the targets’ health.

while True:

    # DISPLAY HERO INFO
    print(f"n{'#' * 18 + '#' * len(current_room)}n You are in the {current_room}.n{'#' * 18 + '#' * len(current_room)}")


    # FIGHT TODO : MAKE IT WORK :|
    def fight():

        boss = Character(name="Minotaur", char_class="Minotaur", hp=50, max_hp=50, damage=4)
        hero = Character(name="Warrior", char_class="Warrior", hp=50, max_hp=50, damage=5)

        hero.attack(boss)
        boss.attack(hero)

        print(f"n{hero.name} has {hero.hp} HP.")
        print(f"{boss.name} has {boss.hp} HP.")

        input()

        if hero.hp == 0:
            death()

        elif hero.hp > 0: # TODO : ITS REPEATING THE ATTACKS, HP NOT DRAINING
            pass

        elif boss.hp == 0:
            boss_list.append(the_map[current_room]["Enemy"])
            print(f"Congratulations! You killed the {boss} and got stronger!")

Except it doesn’t keep looping until someone’s health reaches 0, it just goes back to the first print(), as shown in the image. Not only that, but when I managed to make it kind of loop, it never updated the hp, instead it just repeats the same thing over and over again.

enter image description here

New contributor

Magnus Xavier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT