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()
encounter()setsplayer['hp']less than1, thewhileloop will not repeat.encounter()exits when the condition in itswhileloop is not met. The next time you call it, neither of those values have changed, so it won't go back into the loop.