The first thing I want to talk about is a concept known in programming as Scope.
And this scope is something that we've actually come across already in our programming,
but I wanted to leave it until we've seen functions,
we've seen if and while loops, different types and blocks of code,
before we start talking about this really important concept.
Now, if you want to visualize this concept, imagine you have a house with a fence around it and you
plant an apple tree inside your garden.
Think about who can access those apples, just you and your family, right?
But what if instead you had an apple tree in the neighborhood by the sidewalk, for example?
Well, this is kind of free for all Anybody can go and access those apples.
This is a starting point for how to understand scope.
Take a look at the code that's in the starting file and try to understand what it's doing.
First I create a variable called enemies and I set it to equal 1.
Then I create a function called increase_enemies() and I try to set that variable to 2.
Finally, I call my function triggering all the lines of code inside.
And now I want you to predict what will this print statement print in the console and what will this
print statement print.
And it might not be what you expect it to be.
So I would expect that here I've got a variable which is equal to 1 when this function is called this
variable enemies should now be equal to 2.
So when it's printed down here from outside my function this should be equal to 2,
and this should also be equal to 2.
But look at what happens instead, I get enemies from inside the function equal to 2,
but from the outside the function it's equal to 1.
So what's going on here?
To really understand this, we have to understand the concept of scope.
And scope is a really, really important thing in programming.
You see it in every programming language, and although it's a little bit different between the programming
languages, it all goes back to that concept of the apple tree being within a fence or outside a fence.
So let's take a look at some other examples before we come back to solve this problem.
The first thing to think about is a concept called Local Scope.
Local scope exists within functions.
So for example, if I was creating a game and I had a function called drink_potion(), which is somehow
going to strengthen my player, well, maybe it would have a variable that's created inside the function.
So it could be something like potion_strength,
and I could set that to start off with a value of 2.
Now in this case, if I was to go and print this variable potion_strength from within the function,
notice I'm indented here.
And of course I need to call this function for that to happen.
Notice what happens when I run the code.
It's equal to 2.
That's what we would expect, right?
But notice what happens if I try to print the potion_strength from outside the function here.
This firstly gives me an error right when I'm writing it inside the editor, and when I try to run it,
it gives me an error inside the console.
And the error is a NameError.
And it tells me that the "name 'potion_strength' is not defined."
Clearly, I've defined it here, but why can't I access it outside the function?
Well, this goes back to the concept of local scope.
When you create a new variable or indeed a new function inside another function, it's only accessible,
you can only use it when you're inside that function because it has local scope.
It's only valid within the walls of this drink potion function.
What if we wanted it to be accessible outside the function?
Well, now we need to think about something called Global Scope.
The only difference between global scope and local scope is where you define or where you create your
variables or your functions.
So let's say we have a different variable.
Let's say that I created a variable called player_health,
and it starts off being equal to 10.
Now let's say later on I have the same drink_potion() function and this potion has strength of 2.
So now inside my function, drink_potion(), if I wanted to print my player health even though it wasn't
defined within the function, this is perfectly possible.
And if I call my function drink_potion() and then run the code as it is, you'll see 10 being printed.
So this has a global scope,
it's available anywhere within our file because it was defined at the top level of the file.
Now when I say top level, I don't mean physically at the very top of the file.
I mean that it's not within another function like potion_strength() here, because this is defined indented
inside a function.
So this is a local variable, whereas this is a global variable.
And global variables are available within functions no matter how deep it gets nested.
And it's also available outside of functions.
This concept of global and local scope doesn't just apply to variables.
As I alluded to before, it also applies to functions and basically anything else you name.
This is a concept called the Namespace.
So we defined this variable player_health,
we define this function drink_potion,
anything that you give a name to has a namespace.
And that namespace is valid in certain scopes.
This concept of scope applies to basically anything you name.
If I was to nest this function drink_potion() inside another function, let's call it game(),
well, this drink_potion() now has local scope within the function of game().
So this is why this line is now erroring out because it can't actually see inside this function.
So now in order to call this drink_potion(), I have to be within the four walls of the game() function.
So whenever you give name to anything, a function or a variable, you have to be aware of where you
created it.
Now it's easy to notice where you've created functions.
We have the def keyword right?
But when you create a variable, that's the first time that you give it a name and you set it equal
to something.
And where you write that line of code defines the scope of that particular variable.
Here it's outside of every other function, whereas here it's inside a function actually nested two
levels deep,
so it has local scope to this function.