Trouble with Exercise 43 in LPTHW

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!

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.

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:

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.

1 Like

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.

@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!