0
我做口袋妖怪的基本版本,我有一个功能,看起来像这样:改变改名从函数(蟒蛇)内的全局变量
global userhp, comphp
userhp = 100
comphp = 100
def doturn(attacker, enemy, move, attackerhp, enemyhp):
from random import randint
if move == 1:
dmg = randint(18, 25)
enemyhp -= dmg
print("\t" + attacker, "have done", str(dmg), "damage")
print("\t" + enemy, "now have", enemyhp, "health\n")
if move == 2:
dmg = randint(8, 35)
enemyhp -= dmg
print("\t" + attacker, "have done", str(dmg), "damage")
print("\t" + enemy, "now have", enemyhp, "health\n")
if move == 3:
dmg = randint(15, 22)
attackerhp += dmg
print("\t" + attacker, "have healed", str(dmg), "health")
print("\t" + attacker, "now have", attackerhp, "health\n")
下面是一个例子电话:
doturn(user, comp, 3, userhp, comphp)
我遇到的问题是更改attackerhp和enemyhp不会影响userhp和comphp。它保持在100,游戏无法进展。我需要一种改变全局变量的方式,但也需要这样做,而不是将变量硬编码到函数中,因为这样它只会对用户或计算机有效。
我已经能够解决它,但我的解决方案是返回损坏值并将其应用于userhp和comphp以外的此功能。我可以为用户和comp编写单独的函数,或者通过if语句检查是谁转向它,并且这样做,但我很想保持这种紧凑。对于任何反馈,我们都表示感谢。
缩进没有任何意义。 – khelwood