Python 保存物品时遇到问题
在下面的代码中,我想保存僵尸的生命值,每次我拍摄时从僵尸的生命值中减去50。僵尸的生命值应该是100,并且每一次射击应该从中获得50。我很难把它保存起来。相反,我必须把它打印出来Python 保存物品时遇到问题,python,python-2.7,Python,Python 2.7,在下面的代码中,我想保存僵尸的生命值,每次我拍摄时从僵尸的生命值中减去50。僵尸的生命值应该是100,并且每一次射击应该从中获得50。我很难把它保存起来。相反,我必须把它打印出来 shoot = -50 zombie_hp = -100 def zombie1(shoot, zombie_hp): return shoot - zombie_hp def attack(): print "You see a zombie in the distance." at
shoot = -50
zombie_hp = -100
def zombie1(shoot, zombie_hp):
return shoot - zombie_hp
def attack():
print "You see a zombie in the distance."
attack = raw_input("What will you do?: ")
print zombie1(zombie_hp, shoot)
if attack == "shoot" and shoot == -50:
print "Zombie health 50/100"
attack2 = raw_input("What will you do?: ")
print zombie1(zombie_hp, shoot)
if attack == "shoot":
print "Zombie health 0/100 (Zombie dies)"
attack()
考虑以下几点:
damage = 50
zombie_hp = 100
def attack():
global zombie_hp #1
zombie_hp -= damage #2
print "Zombie health %s/100"%zombie_hp #3
if zombie_hp <= 0:
print "Zombie dies"
print "You see a zombie in the distance."
while zombie_hp > 0: #4
user_input = raw_input("What will you do?: ")
if user_input == 'shoot':
attack()
损坏=50
僵尸_hp=100
def攻击():
全球僵尸hp 1
僵尸的生命-=伤害#2
打印“僵尸健康%s/100”%Zombie#u hp#3
如果僵尸生命为0:4
用户输入=原始输入(“您将做什么?”)
如果用户输入=‘射击’:
攻击()
> @ JrnRaSpe,考虑制作僵尸类。
像这样:class Zombie:
def __init__(self):
self.hp = 100
def attack(target):
target.hp -= DAMAGE
print "Zombie health %s/100"%target.hp
if target.hp <= 0:
print "Zombie dies"
DAMAGE = 50
zombie1 = Zombie()
print "You see a zombie in the distance."
while zombie1.hp > 0:
user_input = raw_input("What will you do?: ")
if user_input == 'shoot':
attack(zombie1)
类僵尸:
定义初始化(自):
self.hp=100
def攻击(目标):
target.hp-=伤害
打印“僵尸健康%s/100”%target.hp
如果目标为hp.0:
用户输入=原始输入(“您将做什么?”)
如果用户输入=‘射击’:
攻击(僵尸1)
“我有麻烦”不是一个有用的问题陈述。你以为会发生什么,结果又发生了什么?您似乎没有试图“让它保存”。您也从不分配由zombie1
返回的值。如果你想保留“僵尸”的状态,可以考虑制作<代码>僵尸类。