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