[Python] why field = max(0,5) does not work and field -= 5 works well? [closed]

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    if i use "hp = max(~ )" print hp result won't change at all

    if i use "hp -= value" print hp result works fine

    Why?

    Stat.py

    class Stat:
    def __init__(self, hp=0, mp=0, dmg=0):
    def __add__(self, other):
    def damage(self, self_instance, value):
    self_instance.base_stat.hp = max(0, self_instance.base_stat.hp - value)
    self_instance.base_stat.hp -= value
    print(f" magic damage")
    if self.hp > 0:
    return True
    else:
    return False


    Full code https://github.com/smc9759/Assignment_OOP

    Skill.py

    from abc import ABC, abstractmethod

    class Skill(ABC):
    def __init__(self):
    @abstractmethod
    def use(self, caster, target):
    pass

    class Spark(Skill):
    def __init__(self):
    def use(self, caster, target):
    skill_dmg = 60 + (caster.stat.dmg * 2)
    target.stat.damage(target, skill_dmg)



    Character.py

    class character(Player):
    def __init__(self, name, role):
    self.name = name
    self.role = role
    self.base_stat = role.base_stat.copy()
    @property
    def stat(self):
    return self.base_stat

    def use_skill(self, target):
    self.role.use_skill(self, target)

    def show_status(self):
    print(f"Name : {self.name}\n HP : {self.stat.hp}\n MP : {self.stat.mp}")


    Role.py

    class Role(ABC):
    def __init__(self, base_stat=ROLE_STAT_CONFIG["Newbie"], skill = None):
    self.base_stat = base_stat


    main.py

    warrior = character("MC_hammer", WARRIOR)

    mage = character("SpoonKiller", MAGE)


    mage.use_skill(warrior)
    warrior.use_skill(mage)

    warrior.show_status()
    mage.show_status()


    i tried = max i expected changes to hp but there was no change

    Continue reading...

Compartilhe esta Página