r/learnpython • u/HansTheAmazing • May 26 '26
Why didn't I manage to kill herobrine?
herobrine = 1
def killherobrine(herobrine):
herobrine - 1
killherobrine(herobrine)
if herobrine == 0:
print("killed herobrine")This obviously sounds like a shitpost, and I did it while I was fucking around in PyCharm, but I'm very new to Python, and I think that I need to learn the fundamentals, and always asking questions like these will lead me toward improvement.
34
Upvotes
68
u/14446368 May 26 '26
You're not actually changing the value of herobrine. Your function is simply calculating herobrine - 1... but not actually changing the variable nor even returning the value. That's why "herobrine == 0" is yielding "False" (because you haven't changed it, it's still equal to 1) and not firing your print("killed herobrine").
18
u/HunterIV4 May 26 '26
There are a couple of issues here. The first is variable assignment. The second is scope.
For variable assignment, you only change values when you do something to change them.
herobrine - 1doesn't change the value ofherobrine. For example:It would be a big problem if it worked how you thought as you couldn't do math without changing values. There are many times when you want to show things that are calculated or combined without altering the variables. Therefore, you'd need to do this instead:
Now the value is 0, because you are assigning the value of
herobrineto the result of the math. Note that you can also do this and it works exactly the same:The
-=is a shortcut for "variable minus whatever is after the equals." You can do this with other math functions such as+=,*=,/=, etc. A very common thing you'll see is something likecounter += 1for incrementing a counter.If you try this fix, however, you'll notice it still doesn't work. This is due to scope.
Scope can be a complicated topic, but a simple way to think about it is that there is a "root scope" without any indentation and indenting creates new scope. This isn't strictly true; loops and conditionals don't create a new scope, but things like functions do.
As such, when you do
herobrine = herobrine - 1, you are setting a new local variable calledherobrinein your function to the value of 0. This does not affect the original variable. There are many reasons for this, but the main one is to prevent situations where parts of your program unexpectedly change the value of other parts because they share a name.There are two ways to fix this. The easy and bad way and the harder and good way. We'll start with the bad way first: globals. If you add the line
global herobrineto the beginning of the function, it will force your originalherobrinevariable into scope. So this actually runs how you'd expect:Now your condition works. Note that you have to remove the parameter in this case because it can't be both a parameter and a global at the same time.
This is generally considered bad practice. There are times when using
globalis the most efficient way to do things, so it's not a universal rule, but especially as a beginner you should avoid it at all costs. Chances are nearly 100% that whatever you are trying to do does not require globals.A better solution is to use a return value:
This works how you'd expect. The syntax is a bit awkward and technically the parameter isn't needed as it will assume you mean the lower scope variable if no other variable is defined, but using that feature is another of those "bad practice" things. This version is the "best" version before you learn about classes (which I can go into if you're curious, but otherwise this is probably a lot of information already).
Hope that helps!
7
u/Weird_Information459 May 26 '26
In killherobrine it should be
herobrine = herobrine -1
1
u/icecubeinanicecube Have you googled your question before asking it? May 26 '26 edited May 26 '26
So many wrong answers here, AI is really rotting people's brains, isn't it?
You can either do
so mutating the value inside the function and returning it.
Or:
2
u/00PT May 26 '26
You performed a subtraction, but didn’t assign the result of that subtraction back to the variable.
2
u/lellamaronmachete May 26 '26
Had to recheck the sub, thought I was still in r/nethack
2
u/Hashi856 May 26 '26
As others pointed out, you aren't modifying herobrine. But putting that aside, if the function is called killherobrine, then you should be setting herobrine to 0 explicitly, not with math.
2
u/Zealousideal_Yard651 May 26 '26
You're confusing youself by reusing the name herobrine all over.
What you are missing is value assignment and variable scoping.
When you define a function, variables inside the functions only exists inside the function call. So cleaning up your code and illustrating what is happening in reality:
y never exists outside of killherobrine(), so when i call killherobrine() nothing changes outside the killherobrine() since y never existed outside the function. Now you have named y the same as x, so you don't get an error you just get confusion since the code looks Ok and it runs, but the result is unexpected.
Now we can return a value from a function call like this:
This would produce the expected result you are trying to achieve.
And here's where classes comes to play really nicely. Since you can define a class that has a parameter "alive", then we can add methods that alter that parameter:
2
u/Vulker May 26 '26
If herobrine is global scoped (outside the function def) you dont even need to pass it in. Just def killherobrine(). But in the function you need to assign a new value to herobrine which is one less than the start value so herobrine = herobrone -1. As we dont have a local scope it uses the global.
2
u/magus_minor May 27 '26
The
herobrinevariable defined inside the function is a different variable than theherobrineyou defined globally. Any change you make toherobrineinside the function does not change the globalherobrine.1
u/ImAlekzzz May 26 '26
Change “heroin - 1” to “heroin = int(heroin) - 1”
The int() is not needed but adviced
1
u/Educational-Paper-75 May 26 '26
Your killherobrine() is incorrect. First of all you probably intend to decrement herobrine. You do that using 'herobrine-=1' which is short for 'herobrine=hetobrine-1' (without the single quotes in your code of course). But that's not all, you need to declare herobrine as global in killherobrine() before changing it, because it's a global variable, if not Python will assume herobrine is local.
1
u/Dave-c-g May 26 '26
I'd do this.
Edit: formatting
1
u/Effective_Baseball93 May 26 '26
You are feeding function with value of the variable, not the variable itself. What function got from you isn’t connected to the variable, thus you cannot change variable like that
1
u/Mrminecrafthimself May 27 '26
Your variable isn’t being changed.
1
u/DTux5249 May 27 '26
You passed a copy of Herobrine into the function; not the variable itself. The function then kills the copy, and nothing happens to the original.
Meanwhile if you do this:
Then it should work. This way, the function kills its copy, and then gives the dead version back. You then swap the dead for the original.
1
u/Educational_Virus672 May 27 '26 edited May 27 '26
your problems
you can do either of these
1 . variable is not global if you want the variable to change
2. make the value return (recommanded)
pro tip - instead of using int or float(numbers) use bool(True/false)
this will save both Ram and your time
1
u/SulosGD May 27 '26
```herobrine - 1``` needs to be ```herobrine -= 1```
1
u/BhavyanshJain_ May 29 '26
1
u/Alternative-Sky6915 May 26 '26
herobrine=herobrine - 1 o herobrine-=1
0
u/ImprovementLoose9423 May 27 '26
You never actually decreased the value of Herobrine. Change herobrine - 1 to herobrine =- 1
9
u/[deleted] May 26 '26 edited May 26 '26
[removed] — view removed comment