Trouble with Exercise 43 in LPTHW


#1

Hi Everybody - I am just looking for a little help with Exercise 43.

My plan at the beginning of this exercise was to not look at the answer as I wrote my own solution to the problem (also without renaming or changing around any of the current objects or functions).

I am a little confused with the Map class, below:


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')

I left out the other parts of the code, because this was my initial focus while trying to build a solution to Exercise 43.

My understanding is that when we create a new class of “Map” called, “a_map,” we then pass the argument ‘central_corridor’ through it (i.e. def init (self, ‘central corridor’)).

However, my confusion corresponds to the following. Once we pass ‘central corridor’ into the init initialization of Map, how can we pass the ‘start_scene’ (‘central corridor’) to the function “def opening scene,” and have it return anything, since we haven’t called the function “opening_scene,” in our program? In other words, do all functions get called whenever a new instance of a class is initialized?

Please let me know what you think - and thank you in advance for your help!


#2

Generally, functions in a class are called when an instance of that class exists and the call is made through it. After the line,

a_map = Map(‘central_corridor’)

has run, you may then make calls to functions from the class. The function is called via:

a_map.opening_scene()

The function that is called when a class member is instantiated is the __init__ function. Other functions can be called when a class is instantiated but most functions you are writing right now will not be. If you really need them to be, the easiest way is to include them within the __init__ function.

The opening_scene function ought to work fine as your have written it. Without the rest of your code, I can’t really say anything about it returning anything, as per your first question.

Hope that helps.


#3

Hi jaroelfsema - thank you for your reply! I ended up looking at the solution, and realized that the opening_scene() is called in the Engine class. I had been flying through the earlier part of the book and just hit a wall with this exercise.

Thank you so much for your response! :grin:


#4

Cool, glad it worked out.

The first part of the book does go fast, especially if you have any experience at all with programming. You’ll probably slow down significantly from this point on, but don’t worry, it’s totally worth the time, in my opinion.


#5

Hitting a wall here is normal, and usually it’s because of one single thing:

When a function is called, you have no idea where the code goes, so when 3 functions are called in a complex order you can’t track it.

That’s most likely what’s going on here, so, take the time to write out (probably on paper) how each function in this code calls other functions.

Also, study this code without any of the textual contents before trying to do it on your own. It’s easier to understand without all the noise of what’s in the rooms. Making tiny smaller models of a problem to study is easiest.


#6

@zedshaw thank you! I’m going to go through it step-by-step today and walk through the code. Thank you for your response Mr. Shaw, I really appreciate it!