例43“;“艰苦学习Python”方法不运行

例43“;“艰苦学习Python”方法不运行,python,Python,我开始创建一个简单的应用程序,该应用程序基于本书作者的框架。我不明白为什么在下面的代码中,类Start的方法enter没有运行,而类的方法enter运行。你能解释一下吗 #!/usr/bin/env python # -*- coding: utf-8 -*- from sys import exit class Engine(object): def __init__(self, scene_map): self.scene_map = scene_map

我开始创建一个简单的应用程序,该应用程序基于本书作者的框架。我不明白为什么在下面的代码中,类
Start
的方法
enter
没有运行,而类
的方法
enter
运行。你能解释一下吗

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import exit

class Engine(object):    

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

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

        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 Scene(object):    

    def enter(self):
        print "not configured yet"
        exit(1)  


class Start(Scene):

    def enter(self):
        print "Enter your name: "
        name = raw_input(" ")

        print "Hi %s. How old are you?" % (name)
        age = raw_input( )

        if age < 18:
            print "Sorry %s... You're too young to play this game." % (name)
            return 'the_end'

        elif age >= 18:
            print "Excellent %s! We can continue! Press enter" % (name)
            raw_input()

        print """Ok %s... I can tell you that it's not a comfortable situation.
        You've lost your memory so here's what happend.
        Today you were eating breakfast on your terrace and suddenly some alien showed from nowhere.
        It has minimized you with its odd gun so now your size is like a match box - more or less.
        Furthermore, the alien has teleported you - you are in some unknown empty house.
        You have to get yourself out in order to come back home, find the alien
        and bring back your initial size.""" % (name)

        raw_input()


class The_end(Scene):
    print "you're dead"
    exit(1)


class Map(object):   

    scenes = {
    "start": Start(),
    "the_end": The_end(),
    }

    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)


a_map = Map("start")
a_game = Engine(a_map)
a_game.play()
#/usr/bin/env python
#-*-编码:utf-8-*-
从系统导入退出
类引擎(对象):
定义初始(自我,场景贴图):
self.scene\u map=scene\u map
def播放(自我):
当前场景=self.scene\u map.opening\u scene()
最后一个场景=self.scene\u map.next\u场景('the\u end'))
当前_场景时!=最后一幕:
下一个场景名称=当前场景。输入()
当前场景=self.scene\u map.next\u场景(next\u场景名称)
当前场景。输入()
类场景(对象):
def输入(自我):
打印“尚未配置”
出口(1)
上课开始(场景):
def输入(自我):
打印“输入您的姓名:”
名称=原始输入(“”)
打印“您好%s。您多大了?”%(姓名)
年龄=原始输入()
如果年龄<18岁:
打印“抱歉%s…您太年轻,无法玩此游戏。”%(名称)
返回“结束”
elif年龄>=18岁:
打印“优秀的%s!我们可以继续!按enter”%(名称)
原始输入()
打印“”“好的%s…我可以告诉您,这不是一个舒适的情况。
你失去了记忆,下面是发生的事情。
今天你在阳台上吃早餐,突然不知从哪里来了一个外星人。
它用它的奇数枪将你最小化,所以现在你的尺寸就像一个火柴盒——或多或少。
此外,外星人已经传送了你——你在某个未知的空房子里。
你必须自己出去才能回家,找到外星人
并返回您的初始大小。“%”(名称)
原始输入()
在_端(场景)初始化:
打印“你死了”
出口(1)
类映射(对象):
场景={
“开始”:开始(),
“the_end”:the_end(),
}
def_uuuinit_uuu(自启动场景):
self.start\u场景=start\u场景
定义下一个场景(自身、场景名称):
val=Map.scenes.get(场景名称)
返回值
def打开_场景(自身):
返回self.next_场景(self.start_场景)
a_map=map(“开始”)
游戏=引擎(地图)
游戏

这将作为运行程序的第一件事执行:

class The_end(Scene):
    print "you're dead"
    exit(1)
你可能想要这样的东西:

class The_end(Scene):

    def enter(self):
        print "you're dead"
        exit(1)

一旦Python经过这些行,您在类级别(对于在模块级别定义的类)中定义的所有内容都将立即执行。当您导入带有类级代码的模块时,也会发生这种情况。

现在已经完全清楚了,谢谢!