簡體   English   中英

while循環內的函數運行不止一次

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

我正在寫一個小游戲,作為嘗試和學習python的一種方式。 在我的代碼的底部,有一個while循環,要求用戶輸入。 如果該用戶輸入為“是”,則應更新一個變量,遇到_問題。 如果遇到問題大於20,則應調用該函數。 我可以使這種行為發生,但是只有一次。 似乎不會再通過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()

它實際上是兩次調用函數encounter ,但是第一次通過將其hp降低到0或以下而最終殺死怪物,而第二次退出該函數卻沒有進入while循環,因為monster['hp'] > 0比較結果為False 每次新遭遇時,怪物的hp不會重置。

如果您在調試代碼時遇到困難,請毫不猶豫地將一些print語句放在不同的位置,以檢查數據的價值。 這應該可以幫助您確定正在發生的事情。

您的代碼中帶有幾個打印命令,以向您展示在尚未完成所有編碼時如何使您看到正在發生的事情。
哦! 我把事情弄平了,不能給你帶來不公平的好處:)

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