好的,所以我一直在学习大部分时间的节目而且我很难过。我环顾四周看看如何调用东西,我得到你可以做如下的事情:
def add(a, b):
return (a + b)
然后你可以通过植入这个来称呼它:
Answer = (1, 4)
输出结果为:5
但就我的情况而言,我正在努力做到这一点并不是很有效。这是我的代码:
import time
def chap4():
print "You feel around the room.\n"
time.sleep(3)
print "You find a small chest...\n"
time.sleep(3)
print "You open the chest...\n"
time.sleep(2)
print "pickaxe", "shovel", "lighter", "9mm(0)", "knife"
while (True):
chest = raw_input("What will you take?: ")
if chest == "pickaxe":
print "You take the pickaxe"
weapon = "pickaxe"
break
elif chest == "shovel":
print "You take the shovel"
weapon = shovel
break
elif chest == "lighter":
print "You take the lighter"
weapon = "lighter"
break
elif chest == "9mm":
print "You take the empty 9mm pistol"
weapon = "9mm"
break
elif chest == "knife":
print "You take the knife"
weapon = "knife"
break
elif chest == "axe":
print "You take the axe"
weapon = "axe"
break
else:
print "Invalid choice. Try again..."
return (chest)
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"
def attack1(target1):
target1.hp -= DAMAGE1
print "Zombie health %s/100"%target1.hp
if target1.hp <= 0:
print "Zombie dies"
def attack2(target2):
target2.hp -= DAMAGE2
print "Zombie health %s/100"%target2.hp
if target2.hp <= 0:
print "Zombie dies"
def attack3(target3):
target3.hp -= DAMAGE3
print "Zombie health %s/100"%target3.hp
if target3.hp <= 0:
print "Zombie dies"
def attack4(target4):
target4.hp -= DAMAGE4
print "Zombie health %s/100"%target4.hp
if target4.hp <= 0:
print "Zombie dies"
def attack5(target5):
target5.hp -= DAMAGE5
print "Zombie health %s/100"%target5.hp
if target5.hp <= 0:
print "Zombie dies"
DAMAGE = 20
DAMAGE1 = 50
DAMAGE2 = 10
DAMAGE3 = 30
DAMAGE4 = 30
DAMAGE = 25
zombie1 = Zombie()
weapon = chap4()
print "You see a zombie in the distance"
while zombie1.hp > 0:
user_input = raw_input("What will you do?: ")
if weapon == "knife":
if user_input == 'stab':
attack(zombie1)
if weapon == "9mm":
if user_input == 'shoot':
attack1(zombie1)
if weapon == "shovel":
if user_input == "smack":
attack2(zombie2)
if weapon == "axe":
if user_input == "chop":
attack3(zombie3)
if weapon == "pickaxe":
if user_input == "mine":
attack4(zombie4)
if weapon == "lighter":
if user_input == "burn":
attack5(zombie5)
当我运行上面的代码时,它会经历第4章,没有任何问题。然而,一旦它进入僵尸部分它就会播放出来,但是当我输入我的方法来杀死它时,它所做的就是无限次地问我“你会做什么”。它永远不会杀死僵尸。我也尝试将weapon = chap4()
放在许多其他地方。这似乎最接近成功。如果可以的话请帮忙。谢谢!
答案 0 :(得分:1)
修复缩进错误后,该代码似乎正常,:
import time
def chap4():
print "You feel around the room.\n"
time.sleep(3)
print "You find a small chest...\n"
time.sleep(3)
print "You open the chest...\n"
time.sleep(2)
print "pickaxe", "shovel", "lighter", "9mm(0)", "knife"
while (True):
chest = raw_input("What will you take?: ")
if chest == "pickaxe":
print "You take the pickaxe"
weapon = "pickaxe"
break
elif chest == "shovel":
print "You take the shovel"
weapon = shovel
break
elif chest == "lighter":
print "You take the lighter"
weapon = "lighter"
break
elif chest == "9mm":
print "You take the empty 9mm pistol"
weapon = "9mm"
break
elif chest == "knife":
print "You take the knife"
weapon = "knife"
break
elif chest == "axe":
print "You take the axe"
weapon = "axe"
break
else:
print "Invalid choice. Try again..."
return (chest)
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"
def attack1(target1):
target1.hp -= DAMAGE1
print "Zombie health %s/100"%target1.hp
if target1.hp <= 0:
print "Zombie dies"
def attack2(target2):
target2.hp -= DAMAGE2
print "Zombie health %s/100"%target2.hp
if target2.hp <= 0:
print "Zombie dies"
def attack3(target3):
target3.hp -= DAMAGE3
print "Zombie health %s/100"%target3.hp
if target3.hp <= 0:
print "Zombie dies"
def attack4(target4):
target4.hp -= DAMAGE4
print "Zombie health %s/100"%target4.hp
if target4.hp <= 0:
print "Zombie dies"
def attack5(target5):
target5.hp -= DAMAGE5
print "Zombie health %s/100"%target5.hp
if target5.hp <= 0:
print "Zombie dies"
DAMAGE = 20
DAMAGE1 = 50
DAMAGE2 = 10
DAMAGE3 = 30
DAMAGE4 = 30
DAMAGE = 25
zombie1 = Zombie()
weapon = chap4()
print "You see a zombie in the distance"
while zombie1.hp > 0:
user_input = raw_input("What will you do?: ")
if weapon == "knife":
if user_input == 'stab':
attack(zombie1)
if weapon == "9mm":
if user_input == 'shoot':
attack1(zombie1)
if weapon == "shovel":
if user_input == "smack":
attack2(zombie2)
if weapon == "axe":
if user_input == "chop":
attack3(zombie3)
if weapon == "pickaxe":
if user_input == "mine":
attack4(zombie4)
if weapon == "lighter":
if user_input == "burn":
attack5(zombie5)
我唯一可以建议的是,你确保为每种武器输入正确的匹配项目,包括案例并避免前导或尾随空格。
您可以通过将最后一部分更改为:
来查看是否存在问题while zombie1.hp > 0:
user_input = raw_input("What will you do?: ")
acted = False
if weapon == "knife" and user_input == 'stab':
attack(zombie1)
acted = True
if weapon == "9mm" and user_input == 'shoot':
attack1(zombie1)
acted = True
if weapon == "shovel" and user_input == "smack":
attack2(zombie2)
acted = True
if weapon == "axe" and user_input == "chop":
attack3(zombie3)
acted = True
if weapon == "pickaxe" and user_input == "mine":
attack4(zombie4)
acted = True
if weapon == "lighter" and user_input == "burn":
attack5(zombie5)
acted = True
if not acted:
print "What, are you NUTS? That won't do anything."