[Tutor] Object and Attribute Error

Alan Thwaits basicbare at gmail.com
Fri Mar 27 17:49:26 EDT 2020


Another error message in my game script from Exercise 43 in Zeb Shaw's
"Learn Python the Hard Way."

The first block of code which the error message references is this:

class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

        #be sure to print out the last scene
        current_scene.enter()

The second block of code referenced is this:

class Map(object):

    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death(),
        'finished': Finished(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name)
        return val

    def opening_scene(self):
        return self.next_scene(self.start_scene)

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

The error message is this:

Traceback (most recent call last):

  File "<ipython-input-1-ceaa7049971f>", line 1, in <module>
    runfile('C:/Users/Alan/Documents/Python/ex43.py',
wdir='C:/Users/Alan/Documents/Python')

  File
"C:\Users\Alan\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
line 827, in runfile
    execfile(filename, namespace)

  File
"C:\Users\Alan\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Alan/Documents/Python/ex43.py", line 194, in <module>
    a_game.play()

  File "C:/Users/Alan/Documents/Python/ex43.py", line 29, in play
    next_scene_name = current_scene.enter()

AttributeError: 'NoneType' object has no attribute 'enter'

Obviously, I need to understand object and attribute better. Any guidance
would be appreciated.

Thanks!

Alan


More information about the Tutor mailing list