0

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()
3
  • If encounter() sets player['hp'] less than 1, the while loop will not repeat. Commented Aug 29, 2016 at 16:03
  • It seems to be repeating. It just does not ever produce a second encounter. Commented Aug 29, 2016 at 16:08
  • encounter() exits when the condition in its while loop is not met. The next time you call it, neither of those values have changed, so it won't go back into the loop. Commented Aug 29, 2016 at 16:11

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code with a couple of print commands to show you how they can let you see what is going on, when you haven't finished all of your coding.
Oh! and I evened things up a bit, can't have you with an unfair advantage :)

import random

def def_monster_attack():
    monster_attack = random.randrange(1,7)
    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': 45,
    '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, Guard, 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 "It's a carve up! Your health", player['hp']," the monster's ", monster['hp']
        else:
            print "Try attacking!"
while player['hp'] > 0 and monster['hp'] > 0:
    action = def_action()

    if action.startswith('y') == True:
        encounter_prob = def_encounter_prob()
        if encounter_prob > 20:
            encounter()
        else:
            print "Nothing happened all seems well"
    else:
        print "What are you going to stand there all day"
    if player['hp'] < 1: print "Oops! You're dead!"
    if monster['hp'] < 1: print "Hurrah! The monster's dead"

1 Comment

Hahaha. I like the additions at the bottom :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.