2014-12-08 2 views
-1

Итак, я учился программировать в течение большей части дня, и я в тупике. Я посмотрел вокруг немного, чтобы увидеть, как называть вещи и я понимаю, что вы могли бы сделать что-то вроде следующего:Как вы называете что-то в Python?

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) 

Когда я запускаю код, он проходит через chap4 без проблем. Однако, как только он доходит до части зомби, он разыгрывается, но когда я вводю свой метод, чтобы убить его, все, что он делает, это спросить меня «что вы будете делать» бесконечно много раз. Он никогда не убивает зомби. Я также попытался разместить weapon = chap4() во многих других местах. Это казалось самым близким к успеху. Пожалуйста, помоги, если можешь. Благодаря!

ответ

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." 
Смежные вопросы