Adjust the Address (Solution)
00:00
If you run the program like it is, you get the output Cookie Cabinet
, Mouse House
and Python Palace
like you explored in one of the former lessons.
00:09
Now, you want line 14 to print Cookie Cabinet
instead of Python Palace
.
00:16
And the challenge now is to do this with one line of code and you’re not allowed to change line 12, but you must add one line inside the function explore_cabinet()
.
00:29
Because of course you could be cheeky and say like, well, I just create a new variable between lines 13 and 14 in front of the print()
function call and set this address to Cookie Cabinet
and call it a day.
00:41
But that’s not what the task is today. Today you need to add one line of code inside of the explore_cabinet()
function. So how do you do that?
00:51
Well, the secret is the global
keyword. With the global
keyword, you can elevate a name from some scope into the global scope. So if you go to the line where you have the docstring with the mouse and press enter for a new line and write global address
, you’re basically saying, I want to address, that’s a coincidence of the words.
01:16
I want to address the address
variable in the global scope instead of the one in the local scope. So in line five, you are not creating a new local scope variable, but you’re overriding the global address
variable by setting global address
in the line before that.
01:36 And that means if you run the code,
01:41
then you can see that the output now is Cookie Cabinet
, Mouse House
and Cookie Cabinet
again. The first print output is the output from line six where you print the address of line five, which is Cookie Cabinet
.
01:55
And the second output Mouse House
is the one in line 10 where you use the variable address
of line eight. And then finally, and that’s the most important part here, once you reach the print()
function call in line 15, the address
variable doesn’t have the value Python Palace
anymore, but Cookie Cabinet
, because after you define address
with Python Palace
in line 13, you are calling the explore_basement()
function where you’re overriding this global address
variable with Cookie Cabinet
and printing it then in line 15, which gives you a third output Cookie Cabinet
.
02:35 So now you have a nice name for your party to take place and hopefully everybody who’s invited will join.
Bartosz Zaczyński RP Team on Oct. 30, 2024
@taniferf It would probably help to step through the code line by line in a debugger to get an idea of what’s going on. Anyway, let’s try to use a pen-and-paper approach.
The code starts at line 13, where you define a global variable and assign an initial value to it. Then, you jump into the outer function, explore_basement()
, and continue on line 8, which defines a local variable with the same name as your global variable. Due to Python’s scoping rules, this local variable shadows the global variable within the function’s scope, meaning the code will use the local version instead of the global one.
Next, you jump into another function, explore_cabinet()
, which tells Python to refer to the already defined global variable instead of creating a variable local to the current function. Consequently, when you assign the value “Cookie Cabinet”, you overwrite the original value stored in the global variable. This has no effect on the variable local to the outer function.
Whenever you leave a function and return to the calling function, Python restores the local variables specific to that function. Unless you explicitly qualified a variable with the global
keyword—or nonlocal
, which is another story—you can generally tell the difference between global and local variables by looking at their scope, which is indicated by their indentation level.
taniferf on Oct. 30, 2024
Thanks!
Become a Member to join the conversation.
taniferf on Oct. 29, 2024
Hi, why the result is not:
I thought that once you use the word
global
on line 4 of the solution code, all other two “address” variable would become like line 5. Why line 10 is not affected and line 15 is?