-1

Really newbie to the code, so I would appreciate your answer, because I didn't find it.

I want to use value (which is hero_health_bar) in couple of assignments, but it just doesn't let me do this, maybe you can give me advice

hero_health_bar = 100

def enemy_attack():
    monster_damage_from_hit = round(random() * 40)
    chance = randint(1, 10)

    if chance >= monster_loose_chance and chance <= monster_crit_chance:

        if (hero_health_bar - monster_damage_from_hit) >= 0:
            hero_health_bar -= monster_damage_from_hit
            print(f'Monster hits you, dealing {monster_damage_from_hit} damage)

1 Answers1

0

you need to pass the health bar to your enemy_attack function, like so

hero_health_bar = 100

def enemy_attack(hero_health_bar):
    monster_damage_from_hit = round(random() * 40)
    chance = randint(1, 10)

    if chance >= monster_loose_chance and chance <= monster_crit_chance:

        if (hero_health_bar - monster_damage_from_hit) >= 0:
            hero_health_bar -= monster_damage_from_hit
            print(f'Monster hits you, dealing {monster_damage_from_hit} damage')

    return hero_health_bar 

Then call it

hero_health_bar = enemy_attack(hero_health_bar)
SuperStew
  • 2,857
  • 2
  • 15
  • 27