def update_health(health):
health = health - 100
return health
sith_health = 100
jedi_health = 100
pick_side() # ignore this
coin_toss() # ignore this
update_health(sith_health)```
I don't know how to update the sith_health variable using the update_health(health) function. The code will return a new health value, but the variable sith_health will remain the same. I did some research, and I read a bit about global varibles (they said it isnt reccomended so I don't realy know how to get sith_health to update). Any help will be appreciated.
#π beginner here...help appreciated.
33 messages Β· Page 1 of 1 (latest)
@wide tide
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
idk how this help system works, so please mention me so that I know that ur here. thanks in advance
its just post ur thing and wait for some1 to answer
after 1h of inactivity it will close tho
oh ok got it
but
#python-discussion message
u can bump ur own thread if no1 answered it and u still need help
just dont spam please
anyways]
well as you have already noted, you return the new value, so you would have to assign the new value to the variable @wide tide
yea I get what you mean, but is there a way to update the varible without using global? I don't understand why people don't advise doing it
there is a very simple way
and its why people dont advise global
i am really lost rn man
!return
Return statement
A value created inside a function can't be used outside of it unless you return it.
Consider the following function:
def square(n):
return n * n
If we wanted to store 5 squared in a variable called x, we would do:
x = square(5). x would now equal 25.
Common Mistakes
>>> def square(n):
... n * n # calculates then throws away, returns None
...
>>> x = square(5)
>>> print(x)
None
>>> def square(n):
... print(n * n) # calculates and prints, then throws away and returns None
...
>>> x = square(5)
25
>>> print(x)
None
Things to note
print()andreturndo not accomplish the same thing.print()will show the value, and then it will be gone.- A function will return
Noneif it ends without areturnstatement. - When you want to print a value from a function, it's best to return the value and print the function call instead, like
print(square(5)).
if u dont understand after reading this just say
it is just shit and ruins the whole concept of functions in my opinion. functions are separated, modularized procedure with an output (potentionally) depending only on the input, being dependent on the global state breaks that.
if you need a state do a class
python you call a function with a copy of the reference, so it is call by value but the value is a reference. when you assign a new value to that reference, then this variable holds a other reference so you can not alter it with assigning, since you can not call a function with the exact reference in python. but what you can is alter the state of an object inside of a function when it is mutable.
!e
class Health:
def __init__(self, health):
self.health = health
def __str__(self):
return str(self.health)
def update_health(health):
health.health -= 100
sith_health = Health(100)
print(sith_health)
update_health(sith_health)
print(sith_health)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 100
002 | 0
so with a wrapper you can do it because you call the function with a copy of the reference so a pointer to the same memory adress so the copy is still a reference to the same object and can alter it
things like
void update_health(int& health) {
health -= 100;
}
int sith_health = 100;
update_health(sith_health);
where you can reference variables and assign a new value directly to the memory address of that variable, with = does not work in python. you can do it in C/C++
ur code is fine for a beginner
but to be honest if the calculaton of the health is not complex i would not make it a function. and if it is assigning the return value is the way to do it in python. in python you want rather create new objects with functions than mutate them in place
but for games like that OOP really fits because it consists out of objects with states you want to mutate and the problem is beginners who do not know OOP yet but sadly know global misuse that to simulate a object with methods that alter a state. their global space is like their object and their functions are like methods of the global state not like functions.
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.