python代码不执行也不提供任何

2024-04-26 14:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的类似zork的游戏,它使用文本命令(更具体地说,在ex43中的learnpython-thehard-way)。 我已经写了所有的代码,可以打破它理解,但代码不能执行。我的代码是:

from sys import exit
from random import randint

class Scene(object):

    def enter(self):
        print "This is not yet configured. Subclass it and implement enter"
        exit(1)


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 = [
        'Shame on you. You lost!',
        'You are such a loser',
        'Even my dog is better at this'
        ]
    def enter(self):
        print Death.quips[randint(0, (len(self.quips) - 1))]
        exit(1) 


class CentralCorridor(Scene):

   def enter(self):
        print "The Gothoms of Planet Parcel # 25 have invade your ship and "
        print "killed all your crew members. You are the last one to survive."
        print "Your last mission is to get the neutron destruction bomb from "
        print "the Laser Weapons Armory and use it to destroy the bridge and get" 
        print "to the escape pod from where you can escape after putting in the" 
        print "correct pod."
        print "\n"
        print "Meanwhile you are in the central corridor where a Gothom stands"
        print "in front of you. You need to kill him if you want to proceed to" 
        print "the Laser Weapons Armory. What will you do now - 'shoot', 'dodge' or       'tell him a joke'"

        action = raw_input(">")

        if action == "shoot":
            print "You try to shoot him but he reacts faster than your expectations"
            print "and kills you"
            return 'death'
        elif action == "dodge":
            print "You thought that you will escape his vision. Poor try lad!"
            print "He kills you"
            return 'death'
        elif action == "tell him a joke":
            print "You seem to have told him a pretty funny joke and while he laughs"
            print "you stab him and the Gothom is killed. Nice start!"
            return 'laser_weapon_armory'
        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'

class LaserWeaponArmory(Scene):

    def enter(self):
        print "You enter into the Laser Weapon Armory. There is dead silence"
        print "and no signs of  any Gothom. You find the bomb placed in a box"
        print "which is secured by a key. Guess the key and gain access to neutron "
        print "destruction bomb and move ahead. You will get 3 chances. "
        print "-----------"
        print "Guess the 3-digit key"
        key = "%d%d%d" % (randint(0, 9), randint(0, 9), randint(0, 9))
        guess_count = 0
        guess = raw_input("[keypad]>")
        while guess_count < 3 and guess!= key:
            print "That was a wrong guess"
            guess_count += 1
            print "You have %d chances left" % (3 - guess_count)
        if guess_count == key:
            print "You have entered the right key and gained access to he neutron"
            print "destruction bomb. You grab the bomb and run as fast as you can"
            print " to the bridge where you must place it in the right place"
            return 'the_bridge'
        else:
            print "The time is over and the Gothoms bomb up the entire ship and"
            print "you die"
            return 'death'

class TheBridge(Scene):

    def enter(self):
        print "You burst onto the bridge with the neutron destruction bomb"
        print "and surprise 5 Gothoms who are trying to destroy the ship "
        print "They haven't taken their weapons out as they see the bomb"
        print "under your arm and are afraid by it as they want to see the"
        print "bomb set off. What will you do now - throw the bomb or slowly place the bomb"
        action = raw_input(">")
        if action == "throw the bomb":
            print "In a panic you throw the bomb at the Gothoms and as you try to"
            print "escape into the door the Gothoms shot at your back and you are killed."
            print "As you die you see a Gothom trying to disarm the bomb and you die"
            print "knowing that they will blow up the ship eventually."
            return 'death'
        elif action == "slowly place the bomb":
            print "You inch backward to the door, open it, and then carefully"
            print "place the bomb on the floor pointing your blaster at it."
            print "Then you immediately shut the door and lock the door so"
            print "that the Gothoms can't get out. Now as the bomb is placed"
            print "you run to the escape pod to get off this tin can."
            return 'escape_pod'
class EscapePod(Scene):

    def enter(self):
        print "You rush through the whole ship as you try to reach the escape pod"
        print "before the ship explodes. You get to the chamber with the escape pods"
        print "and now have to get into one to get out of this ship. There are 5 pods"
        print "in all and all are marked from 1 to 5. Which escape pod will you choose?"
        right_pod = randint(1, 5)
        guess = raw_input("[pod#]>")

        if int(guess) != right_pod:
            print "You jump into the %s pod and hit the eject button" % guess
            print "The pod escape into the void of space, then"
            print "implodes as the hull ruptures crushing your"
            print "whole body"
            return 'death'
        elif int(guess) == right_pod:
            print "You jump into the %s pod and hit the eject button" % guess
            print "The pod easily slides out into the space heading to the planet below"
            print "As it flies to the planet you see the ship exploding"
            print "You won!"

            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()

我在cmd中键入以下命令:

^{pr2}$

但它不打印一行也不给任何行错误。这个会发生什么

^{3}$

Tags: andthetoselfyoureturndefscene
2条回答

ajkumar25的代码对我也有用。如果出现任何错误,可以尝试捕获所有异常以查看:

try:
    # all your code here
except Exception as e:
    print e
finally:
    raw_input("Press any key to exit...")

您正在将str传递给Engine,但它接受object。这就是这个错误出现的原因。只需将对象传递给Engine,就可以开始了。在

'str' object has no attribute 'opening_scene'

我帮你修好了。现在它正常工作了

^{pr2}$

输出

      -
The Gothoms of Planet Parcel # 25 have invade your ship and 
killed all your crew members. You are the last one to survive.
Your last mission is to get the neutron destruction bomb from 
the Laser Weapons Armory and use it to destroy the bridge and get
to the escape pod from where you can escape after putting in the
correct pod.


Meanwhile you are in the central corridor where a Gothom stands
in front of you. You need to kill him if you want to proceed to
the Laser Weapons Armory. What will you do now - 'shoot', 'dodge' or       'tell him a joke'
>

相关问题 更多 >