【发布时间】: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