Function within while loop not running more than once
Question: I am writing a small game as a way to try and learn python. At the bottom of
my code, there is a while loop which asks for user input. If that user input
is yes, it is supposed to update a variable, encounter_prob. If encounter_prob
is above 20, it is supposed to call the function. I can get this behavior to
happen, but only once. It seems to not be going through the if statement
again.

    
    
    import random
    
    def def_monster_attack():
        monster_attack = random.randrange(1,6)
        return monster_attack
    
    def def_player_attack():
        player_attack = random.randrange(1,7)
        return player_attack
    
    def def_encounter_prob():
        encounter_prob = random.randrange(1,100)
        return encounter_prob
    
    def def_action():
        action = raw_input("Move forward? Yes or No")
        return action
    
    player = {
        'hp': 100,
        'mp': 100,
        'xp': 0,
        'str': 7
        }
    
    monster = {
        'hp': 45,
        'mp': 10,
        'str': 6
        }
    
    def encounter():
        while player['hp'] > 0 and monster['hp'] > 0:
    
            keep_fighting = raw_input("Attack, Spell, Gaurd, or Run?")
            if keep_fighting.startswith('a') == True:
                player_attack = def_player_attack()
                monster['hp'] = monster['hp'] - player_attack
                monster_attack = def_monster_attack()
                player['hp'] = player['hp'] - monster_attack
                print "player's hp is", player['hp']
                print "monster's hp is", monster['hp']
    
    while player['hp'] > 0:
        action = def_action()
    
        if action.startswith('y') == True:
            encounter_prob = def_encounter_prob()
            if encounter_prob > 20:
                encounter()
    


Answer: It's actually calling the function `encounter` twice but the first time ends
up killing the monster by decreasing its `hp` to 0 or below, while the second
time the function exits without entering the `while` loop because the
`monster['hp'] > 0` comparison evaluates to `False`. The monster's `hp` aren't
reset at each new encounter.

If you're having difficulties debugging your code, do not hesitate to put some
`print` statements in different places to examine the value of your data. This
should help you pinpointing what's going on.

