AttributeError:None类型对象没有属性“enter”

2024-04-28 04:11:10 发布

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

我想写一个游戏,你要经历9层地狱,目的是要通过“艰难的学习Python”。然而,我只是不明白为什么我在尝试进入游戏的下一个场景时会收到一条错误消息:

class Scene(object): # class Scene has-a function named enter

    def enter(self): # create enter function
        pass


class Engine(object):

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

    def play(self):
        last_scene = self.scene_map.next_scene('Lust')
        current_scene = self.scene_map.opening_scene()

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

            current_scene.enter()


class Death(Scene):

    def enter(self):
        pass


class Limbo(Scene):

    def enter(self):
        print """               In Limbo reside the unbaptized and the virtuous pagans
                who, although not sinful, did not accept Christ.
                Without baptism, they lacked the hope for something
                greater than rational minds can conceive.
                You find yourself within Limbo, even though you weren't
                a Pagan.  How strange!  You look around, and see a man
                that looks like someone important, but you don't know
                who he is.  What do you do?"""
        action = raw_input("> ")
        if raw_input == "Walk around" or raw_input == "walk around":
            print "You walk around for a bit, admiring the scenery.  You get bored quickly."
        elif raw_input == "talk to the man":
            print ""
        else:
            print 'Lust'


class Lust(Scene):

    def enter(self):
        pass


class Map(object):

    scenes = { # create a dictionary of the scenes you want to use
        'limbo': Limbo(),
        'lust': Lust(),
    }

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

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

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

当我运行它时,我可以遍历第一个“级别”,但是当我到达操作提示符并输入一些内容时,它就会取消,并显示错误代码:

^{pr2}$

不知道为什么。


Tags: thenameselfyoumapdefcurrentscene