简体   繁体   中英

Function within while loop not running more than once

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()

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM