Aside from indentation issues in the Pastebin, this won't work as written because there are two different variables named hp
in play here.
One of the hp
variables is in the outer "scope", and the other is in the inner "scope", or is in the function's scope. This means that the hp
that is set to 10 is accessible anywhere outside of the function you created. It's what's known as a "global" variable.
When you attempt to write to hp
inside of the function, you are creating the second variable named hp
that is used instead of the other hp
variable. In other words, the inner hp
"shadows" the outer one.
I won't get into the reasons that this happens, but just know that you're basically operating from a clean slate when you enter a function.
If you want to write to the global hp
variable, you have to explicitly tell the script that you want to do that, using the global
keyword:
a = 0
def increment_a():
global a
a += 1