Coastharukun 2016-08-21 14:11 采纳率: 0%
浏览 1759

【Python求教】类和继承函数调用出错

最近在学“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()
  • 写回答

2条回答 默认 最新

  • devmiao 2016-08-21 14:18
    关注

    self.scene_map为空对象造成的。

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办