最近在学“Learn Python the hard way”,这是其中的一个练习。
在运行的时候报错,AttributeError: 'NoneType' object has no attribute 'enter'。
想不明白为什么current_scene的返回值是None。
求大神指导,
下面是代码:
from sys import exit
from random import randint
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()
current_scene = self.scene_map.next_scene(next_scene_name)
class Death(Scene):
quips = [
"You died. You kinda suck at this.",
"Your mom would be pround...if she were smarter.",
"Such a luser."
"I have a small puppy that's better at this."
]
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class CentralCorridor(Scene):
def enter(self):
print "*******"
action = raw_input('>')
if action == "shoot!":
print "@@@@"
return 'death'
else:
return 'centralcorridor'
class Map(object):
scenes = {
'centalcorridor':CentralCorridor(),
'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)
a_map = Map('centralcorridor')
a_game = Engine(a_map)
a_game.play()