Coming back to our original example.
I want to talk more about the global scope and the concept of modifying
something within the global scope. Here we have enemies,
which is a variable that has global scope,
and here we have a function which creates a local scope.
Now we think that we're tapping into this variable and setting it to two.
But in fact, we're actually creating a completely new variable
that's entirely separate from this one.
And this is why when we printed it here,
it showed it was equal to 2. But when we printed it here, it showed
it was actually equal to 1. If you want to make this a little more obvious,
I can call this a skeleton and I can set this to be a zombie.
And when I hit run, you can see again, this prints zombie,
this prints skeleton,
because these two variables are actually entirely different things.
And we're not changing this right here at all,
we're just creating a new variable that has a local scope.
Now it's usually a terrible idea to call your local variables and your global
variables the same name. But in this case,
what we actually wanted to do was we wanted to modify this variable,
we wanted to do something like maybe += 1. And notice how
as soon as I write += 1,
that my editor starts going crazy and tells me that this local variable enemies
is defined in an enclosing scope is referenced before assignment.
What does that mean?
It means that the editor thinks you're trying to tap into a local variable that
you defined somewhere around here.
And then you tried to modify it by adding one to the previous value,
but you actually haven't defined it. What we wanted to do though,
is we wanted to tap into this variable and change it here.
In order to do this,
we actually have to explicitly say that we have a global variable
which is called enemies that's defined somewhere outside of this function.
And that is the enemies that we want to use inside this function.
So it basically takes that global enemies into the function and allows you to
modify it. Without this line of code
we cannot modify something that is global within a local scope.
Now there's a reason why it's so difficult to modify something that has global
scope.
You probably don't actually want to do this very often, because it's confusing and
it's prone to creating bugs and errors. Because this variable with global scope
could have been created anywhere in your code, right?
And you would be modifying it completely independent of when you created it.
So it might've been days between when you wrote this code and when you wrote
this code and it just makes everything more fallible,
more easy to fail.
This is why very often people will tell you when they're teaching you Python to
avoid modifying global scope. You can read it,
that's not a problem. You can use it within your code, like we are here.
But don't try to modify it within a function that has local scope.
But what can you do instead?
What if you wanted to have this functionality like a function that changes the
number of enemies?
How can you achieve this without modifying the global scope within the function?
Well, you could use what we learned about return statements instead,
right? What if instead of modifying the enemies,
you actually just simply returned it as the output.
So return the current value of enemies + 1.
Now once you call this function,
you'll get hold of the output and you can save it to the global variable enemies.
So this now means that this function can be taken away and placed anywhere in
your code and you don't actually need to know how it works.
As long as you know that this is how you increase enemies,
then all you have to do is just call it.