所以我一直在研究Zed Shaw的Learn Python The Hard Way并且在练习43之前取得了一些相当不错的成功,这个练习使用面向对象编程原理来创建一个非常简单的基于文本的游戏。我曾多次被得到一个属性错误,更specifially:如何在Python中调试AttributeError?
File "PracticeGame.py", line 206, in <module>
a_game.play()
File "PracticeGame.py", line 20, in play
next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
我已经看到关于此错误的多个职位,但没有一个答案都真正解释的方式我能理解这个问题,也没有提供的解决方案为我工作。以下是从代码行包括20行:
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()
while True:
print "\n-------"
next_scene_name = current_scene.enter() #this is line 20
current_scene = self.scene_map.next_scene(next_scene_name)
return current_scene
这是206
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play() #line 206
作为图被限定的代码,包括行的末尾:
class Map(object):
scenes = {
'central_cooridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod(),
'death': Death()
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
return Map.scenes.get(scene_name)
def opening_scene(self):
return self.next_scene(self.start_scene)
我从明白不同的帖子,这个错误消息意味着第20行的一部分没有定义,但我失去了什么没有定义以及为什么发生。我是Python的新手。
请发表'self.scene_map.opening_scene()'或'Map'的定义 – dm03514
我刚加入帖子,在评论中看起来不太好 – Woomfy