【问题标题】:def and return error in python2python2中的def和返回错误
【发布时间】:2017-12-20 02:05:44
【问题描述】:

嘿,我正在开发一个控制台游戏,但我遇到了一个问题。首先,程序应该如下工作: 用户选择攻击力, 规模为 M 的攻击将以 (100-M)% 的几率成功。也就是说,更高的幅度意味着更高的错过它的风险。比如M是30,成功的几率是70%,而M是10,成功的几率是90% :

import random


def attack1():
    hp2=100
    chance_of_damaging=random.randint(0,100)
    print "your chance of damaging", chance_of_damaging
    while True:
        attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")

        if attack_magnitute > 50:
            print "The attack magnitude must be between 1 and 50."

        elif attack_magnitute < 1:
            print "The attack magnitude must be between 1 and 50."
        else:
            break


    while True:
        if chance_of_damaging > attack_magnitute:
            print "attack is succesful"
            hp2=hp2-attack_magnitute
            return hp2
        else:
            print "attack is unsuccesful"
            return hp2
print  attack1()


def attack2():
    hp1=100
    chance_of_damaging=random.randint(0,100)
    print "your chance of damaging", chance_of_damaging
    while True:
        attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")

        if attack_magnitute > 50:
            print "The attack magnitude must be between 1 and 50."

        elif attack_magnitute < 1:
            print "The attack magnitude must be between 1 and 50."
        else:
            break


    while True:
        if chance_of_damaging > attack_magnitute:
            print "attack is succesful"
            hp1=hp1-attack_magnitute
            return hp1
        else:
            print "attack is unsuccesful"
            return hp1
print attack2()

程序应继续运行,直到其中一个 hp 值等于 0 当我尝试调用 hp1 或 hp2 变量时,该值被删除。

有人可以帮我解决我的错误吗?我必须在 30 小时内提交我的项目。提前致谢。

【问题讨论】:

  • 对不起,我没有解释清楚。代码正在运行,但程序应该继续运行,直到其中一个 hp 值等于零。所以我尝试调用攻击 1() 和攻击 2() 函数直到其中一个 hp 值等于 0。如果你有机会调试它,你会理解我的。例如在第一次攻击中:hp1 正在减少 80,并且 hp2=90 从 100。但是程序不记得第二轮中 hp 的最后一个值。
  • 啊,好的。因此,您想继续循环遍历攻击函数,直到其中一个 hp 为 ==0 ?意思是,attack1 或 attack2 可能不止一次被调用?
  • 是的,正是如此。我该怎么做?

标签: python-3.x python-2.7 function return


【解决方案1】:

看底部新的main函数,hp值是传入并返回的,而不是在攻击函数开始时重新初始化。循环继续,直到其中一个 hps

import random


def attack1(current_hp):
    hp2=current_hp
    chance_of_damaging=random.randint(0,100)
    print "your chance of damaging", chance_of_damaging
    while True:
        attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")

        if attack_magnitute > 50:
            print "The attack magnitude must be between 1 and 50."

        elif attack_magnitute < 1:
            print "The attack magnitude must be between 1 and 50."
        else:
            break


    while True:
        if chance_of_damaging > attack_magnitute:
            print "attack is succesful"
            hp2=hp2-attack_magnitute
            return hp2
        else:
            print "attack is unsuccesful"
            return hp2



def attack2(current_hp):
    hp1=current_hp
    chance_of_damaging=random.randint(0,100)
    print "your chance of damaging", chance_of_damaging
    while True:
        attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")

        if attack_magnitute > 50:
            print "The attack magnitude must be between 1 and 50."

        elif attack_magnitute < 1:
            print "The attack magnitude must be between 1 and 50."
        else:
            break


    while True:
        if chance_of_damaging > attack_magnitute:
            print "attack is succesful"
            hp1=hp1-attack_magnitute
            return hp1
        else:
            print "attack is unsuccesful"
            return hp1
def main():
     hp1, hp2 = (100,100)
     while hp1 > 0 and hp2 > 0:
        hp1 = attack1(hp1)
        hp2 = attack2(hp2)
main()

编辑: 如果你想打印出 hp1 和 hp2 的最终状态,让你的 def main() 像这样:

def main():
     hp1, hp2 = (100,100)
     while hp1 > 0 and hp2 > 0:
          hp1 = attack1(hp1)
          hp2 = attack2(hp2)
     print "Final State of hp1: " + str(hp1)
     print "Final State of hp2: " + str(hp2)

【讨论】:

  • 您也可以摆脱其他攻击方法,因为唯一的区别因素是 hp 作为内部状态。您可以只有一个 attack() 方法,并在主循环中对其进行两次调用。只需为每个调用传入不同的 hp 值。
  • 我的猜测是你试图调用 hp1 或 hp2 超出范围。确保在 main() 函数中打印 hp1 和 hp2 的值。
  • 每次攻击后你想显示当前的hp吗?
  • 是的,我做到了。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2017-02-23
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多