#πŸ”’ beginner here...help appreciated.

33 messages Β· Page 1 of 1 (latest)

wide tide
#
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.
half tartanBOT
#

@wide tide

Python help channel opened

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.

wide tide
#

idk how this help system works, so please mention me so that I know that ur here. thanks in advance

kindred swan
#

after 1h of inactivity it will close tho

wide tide
#

oh ok got it

kindred swan
#

but

#

#python-discussion message

#

u can bump ur own thread if no1 answered it and u still need help

#

just dont spam please

#

anyways]

dull shell
#

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

wide tide
kindred swan
#

and its why people dont advise global

wide tide
#

i am really lost rn man

kindred swan
#

!return

half tartanBOT
#
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() and return do not accomplish the same thing. print() will show the value, and then it will be gone.
  • A function will return None if it ends without a return statement.
  • 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)).
kindred swan
#

if u dont understand after reading this just say

dull shell
#

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)
half tartanBOT
dull shell
#

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++

wide tide
#

i see, ill make sure to revise my code

#

thanks guys, I really appreciate it

kindred swan
dull shell
#

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.

half tartanBOT
#
Python help channel closed for inactivity

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.