学习Python艰难的方法,练习43 object()不带参数

时间:2015-06-26 00:49:18

标签: python

我正在使用Zed的Learn Learnthonthehardway HTML版本。我目前正在练习#43,但是我已经从游戏中删除了一些场景,而且我遇到了问题。

我收到错误:"object() takes no parameters" at the line 'a_map = Map('empty_scene')

这是我的代码:

from sys import exit

class Scene(object):

def enter(self):
    print "This scene 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()
    last_scene = self.scene_map.next_scene('finished')

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

def enter(self):
    pass

class FinishScene(Scene):

def enter(self):
    pass

class Map(object): 

scenes = {
    'empty_scene': EmptyScene(),
    'finished': FinishScene(),
}

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('empty_scene')
a_game = Engine(a_map)
a_game.play()

1 个答案:

答案 0 :(得分:2)

你的缩进看起来很远。

class EmptyScene(Scene):

def enter(self):
    pass

应该是:

class EmptyScene(Scene):

    def enter(self):
        pass

Map与_init_并不真正关联,_init_应该是__init __

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

因此,不是调用Map._init_,而是Map(xxx)最终调用object .__ init__,它没有参数,并且没有考虑你自己的函数定义。

尝试:

class Map(object): 

    scenes = {
        'empty_scene': EmptyScene(),
        'finished': FinishScene(),
    }

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

当你在它的时候,缩进所有你有自我的def(意思是方法),4个空格在这些类下,因为你的整个代码都有这个问题。

记住:缩进4个空格和双重下划线到__init__方法。

祝你好运