如何从dict变量调用fucntion

时间:2017-06-26 11:05:41

标签: python

您好我很抱歉这样的问题,但我正在学习python,我正在使用名为[学习python艰难的方式]的书。在练习43_Classes.py我无法弄清楚为什么我的输出提供了无效的语法我希望有人在这里指出这个脚本出了什么问题谢谢你。

from sys import exit
from random import randint

class Scene(object):

    def enter(self):
        pass


class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n--------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

class Death(Scene):

    quips = [
        "You dies.  You kinda suck at this.",
        "Your mum would be proud...if she were smarter.",
        "Such a loser!",
        "I have a small puppy that would be better at this than you.",
        "Wasted Non-GTA (ref).",
        "Gothon's rule!"
    ]

    def enter(self):
        print Death.quips[randint(0, len(self.quips)-1)]
        exit(1)

class CentralCorridor(Scene):

    def enter(self):
        print "The Gothons of planet percal #25 have invaded your ship and destroyed"
        print "your entire crew. You are the last surviving member and you last"
        print "mission is to get the neutron destruct bomb from the weapons armory."
        print "put it in the bridge, and blow the ship up after getting into an "
        print "escape pod."
        print "\n"
        print "You're running down the central corridor to the weapons armory when"
        print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume"
        print "flowing around his hate filled body. He's blocking the door to the"
        print "Armory and about to pull a weapon to blast you."

        action = raw_input("> ")

        if action == "shoot!":
            print "Quick on the draw you yank out your blaster and fire it at the Gothon."
            print "His clown costume is flowing and moving around his body, which throws"
            print "off your aim. Your laser hit his costume but misses him entirely. This"
            print "completely ruins his brand new costume his mother bought him, which"
            print "makes him fly into a rage and blast you repeatedly in the face until"
            print "you are dead. Then he eats you."
            return 'death'

        elif action == 'dodge!':
            print "Like a world class boxer you dodge, weave, slip and slide right"
            print "as the Gothon's blaster cranks a laser past your head."
            print "In the middle of your artful dodge your foot slips and you"
            print "bang your head on the wall and pass out."
            print "you wake up shortly after only to die as the Gothon stomps on"
            print "your head and eats you."
            return 'death'

        elif action == 'tell a joke' or 'cheat':
            print "Lucky for you they made you learn Gothon insults in the academy."
            print "You tell the Gothon a joke you know: "
            print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
            print "The Gothon stops, tries not to laugh, then bust out laughing and can't move."
            print "While he's laughing you run up and shoot him square in the head"
            print "putting him down, then jump through the Weapon Armory door."
            return 'laser_weapon_armory'

        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'




class LaserWeaponArmory(Scene):

    def enter(self):
        print "You do a dive roll into the Weapon Armory, crouch and scan the room"
        print "for more Gothons that might be hidding. It's dead quiet, too quiet."
        print "You stand up and run to the far side of the room and find the"
        print "neutron bomb in its container. There's a keypad lock on the box"
        print "and you need the code th get the bomb out. If you get the code"
        print "wrong 10 times then the lock closes forever and you can't"
        print "get the bomb. The code is 3 digits."
        print "Deactivate my bomb c**t!"
        code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
        guess = raw_input("[Keypad]> ")
        guesses = 0
        attempts = 10

        while guess != code and guesses < 9:
            guesses += 1
            attempts -= 1
            cheat = 000
            print "Invaid key!\nAttempts left (%d)" % attempts
            guess = raw_input("[Keypad]> ")
            guess_01 = guess

            if guess == code or guess_01 :
                print "the container clicks open and the seal breaks, letting gas out."
                print "You grab the neutron bomb and run as fast as you can to the"
                print "bridge where you must place it in the right spot."
                return 'the_bridge'

            elif guess != code and guesses < 9:
                pass

            else:
                print "The lock buzzes one last time and then you hear a sickening"
                print "melting sound as he mechanism is fused together."
                print "You decide to sit there, and finally the Gothons blow up the"
                print "ship from their ship and you die."
                return 'death'




class TheBridge(Scene):

    def enter(self):
        print "You burst onto the Bridge with the neutron destruct bomb"
        print "under your arm and suprise 5 Gothons who are trying to"
        print "take control of the ship. Each of them has an even uglier"
        print "clown costume than the last. They haven't pulled their"
        print "weapons out yet, as they see the active bomb under your"
        print "arm and don't want to set it off."

        action = raw_input("> ")

        if action == "throw the bomb":
            print "The bomb explodes!"
            return 'death'

        elif action  == 'slowly place the bomb' or 'cheat':
            print "You run to the escape pods"
            print "and leave the bomb behind..."
            return 'escape_pod'

        else:
            print "This does not compute!"
            return 'death'

class EscapePod(Scene):

    def enter(self):
        print "There are 5 pods which one?"
        good_pod = randint(1, 5)
        guess = raw_input(">  ")

        if int(guess) != good_pod:
            print "You jump into pod number: %s" % guess
            print "You are stuck the pod wont open your stuck inside!"
            return 'death'

        else:
            print "Yay you shoot out and the ship blows up you win!"
            return 'finished'

class Map(object):

    scenes = {
        "central_corridor": CentralCorridor(),
        "laser_weapon_armory": LaserWeaponArmory(),
        "the_bridge": TheBridge(),
        "escape_pod": EscapePod(),
        "death": Death()
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)

    def opening_scene(self):
        return self.next_scene(self.start_scene)


a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

1 个答案:

答案 0 :(得分:1)

我想在你的Map课程中想要一本字典:

class Map(object):

    scenes = {
        "central_corridor": CentralCorridor(),
        "laser_weapon_armory": LaserWeaponArmory(),
        "the_bridge": TheBridge(),
        "escape_pod": EscapePod(),
        "death": Death()
    }

圆括号用于词典,这是键对值。