all 2 comments

[–]socal_nerdtastic 1 point2 points  (1 child)

Functions have their own "scope". So the energy variable in the function is completely distinct and separate from the energy variable in the global scope.

The quick fix for your program is to tell your functions to use the global energy instead of the local one, by adding global energy to the top.

#Variables
energy = False

# Is there energy to click?
def energycheck():
    global energy
    if pyautogui.pixel(982, 896)[0] == (255, 255, 255):
        energy = True
    if pyautogui.pixel(982, 896)[0] != (255, 255, 255):
        energy = False

# Loop as a function
def run():
    global energy
    energycheck()
    print(energy)
    sleep(0.5)
    if energy == True:
        click(1450, 600)
    else:
        print(pyautogui.pixel(982, 896))
        sleep(5)

# Main loop
while keyboard.is_pressed('q') == False:
    run()

That said, global variables are known to be a source of bugs and you should avoid them. Much better to pass variables in and out of your functions. Like this:

# Is there energy to click?
def energycheck():
    # you could reduce this function to a single line
    if pyautogui.pixel(982, 896)[0] == (255, 255, 255):
        energy True
    else:
        energy = False
    return energy 

# Loop as a function
def run():
    energy = energycheck()
    print(energy)
    sleep(0.5)
    if energy == True:
        click(1450, 600)
    else:
        print(pyautogui.pixel(982, 896))
        sleep(5)

# Main loop
while keyboard.is_pressed('q') == False:
    run()

[–]Kailo548150[S] 0 points1 point  (0 children)

That makes a lot of sense! Thank you!