Global variables not working in functions

Hello, first time on here. I am confused why only the health variable works and not the others. Is it because the others aren’t numbers? And if this the case, why does yes not work? Does it have something to do with the fact that yes isn’t a positive number?

global health = 50
global maxhealth = 100
global minhealth = 1
global fed = False
global watered = False
global played = False
global walks = False
global reads = False
global yes = 0
def daywait():
    yes = 1 #does not work
    time.sleep(2) #works
    health -= randint(4,21) #works
    print(f"Welcome back! Your pets health is how {health}") #works
    fed = False #does not work
    watered = False #does not work
    played = False #does not work
    walks = False #does not work
    reads = False #does not work
    yes = 0 #does not work
def healthup(): #works
    health += randint(10,25) #works

This is not at all valid python code and if you try to run it, the interpreter will yell at you. It sounds like the code does something on your system, so you aren’t running stock python.

Hello, sorry, I didn’t know if I needed to specify but I am using VS Code and the extensions I have installed are:
Pylance
Python (3.11)
Python Debugger
Python Environments
Python Indent

And I also imported random and time.

If you execute the above code, what is the output in the console?

Hello, sorry for wasting your time but I have realised that I just made a simple error where I forgot to put “global” before all of the variables in the function. Thank you for trying to help and once again, sorry for wasting your time.

In addition, using global at the global scope does nothing useful. You only need the global statements in the function, not in conjunction with the ones you already have.

3 Likes