python2中的def和return错误

时间:2017-12-20 02:05:45

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

嘿我正在制作一个控制台游戏而且我遇到了问题。首先,该程序应该如下工作: 用户选择攻击力, 大小为M的攻击将成功,并且可能达到(100-M)%。也就是说,更高的幅度意味着错过它的风险更高。例如,如果M为30,则成功的几率为70%,而M为10,成功的概率为90%。如果攻击成功,hp将减少,程序将显示hp的玩家。我写了一些东西:

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值等于零 当我尝试调用hp1或hp2变量时,该值将被删除。

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

1 个答案:

答案 0 :(得分:0)

查看底部的新主要功能,hp值被传入并返回,而不是在攻击函数开始时重新初始化。循环继续,直到其中一个hps <= 0

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)