如何在下面给出的代码中修复此语法错误?

时间:2016-01-30 18:23:21

标签: python python-2.7

代码是 -

class Map(object):

scenes = [
    'central_corridor': CentralCorridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'death': Death()
]

python解释器停留在第34行; ' central_corridor' :CentralCorridor()"其中:(冒号)被标记为语法错误。场景是程序中早期所有类的列表。

2 个答案:

答案 0 :(得分:2)

您是否尝试创建dictionary?如果是这样,它应该用大括号{ }括起来:

scenes = {"central_corridor": CentralCorridor()}

方括号[ ]用于lists

答案 1 :(得分:0)

代码中至少有两个语法错误。

  • Python类必须包含至少一个缩进行。
  • 字典被{}包围,而不是[]

我最好的猜测是,这就是你想要的。

class Map(object):

    def __init__(self):
        self.scenes = {
            'central_corridor': CentralCorridor(),
            'laser_weapon_armory': LaserWeaponArmory(),
            'the_bridge': TheBridge(),
            'escape_pod': EscapePod(),
            'death': Death(),
        }