简体   繁体   English

while循环内的函数运行不止一次

[英]Function within while loop not running more than once

I am writing a small game as a way to try and learn python. 我正在写一个小游戏,作为尝试和学习python的一种方式。 At the bottom of my code, there is a while loop which asks for user input. 在我的代码的底部,有一个while循环,要求用户输入。 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. 如果遇到问题大于20,则应调用该函数。 I can get this behavior to happen, but only once. 我可以使这种行为发生,但是只有一次。 It seems to not be going through the if statement again. 似乎不会再通过if语句。

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 . 它实际上是两次调用函数encounter ,但是第一次通过将其hp降低到0或以下而最终杀死怪物,而第二次退出该函数却没有进入while循环,因为monster['hp'] > 0比较结果为False The monster's hp aren't reset at each new encounter. 每次新遭遇时,怪物的hp不会重置。

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. 如果您在调试代码时遇到困难,请毫不犹豫地将一些print语句放在不同的位置,以检查数据的价值。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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