Function within while loop not running more than once
<p>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.</p>

<pre><code>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'] &gt; 0 and monster['hp'] &gt; 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'] &gt; 0:
    action = def_action()

    if action.startswith('y') == True:
        encounter_prob = def_encounter_prob()
        if encounter_prob &gt; 20:
            encounter()
</code></pre>

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

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

python python-2.7