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

2024-04-16 20:30:46 发布

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

代码是-

class Map(object):

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

python解释器停留在“central_corridor”:CentralCorridor()”行,其中:(冒号)被标记为语法错误。Scenes是程序中先前生成的所有类的列表。在


Tags: the代码mapobjectclassbridgecentrallaser
2条回答

你想创建一个dictionary?如果是这样,应该用大括号{ }

scenes = {"central_corridor": CentralCorridor()}

方括号[ ]用于lists。在

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

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

我猜这就是你想要的。在

class Map(object):

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

相关问题 更多 >