H
H
hesy2018-05-15 18:10:58
Python
hesy, 2018-05-15 18:10:58

My first time with "local variable 'WITH_GIRL' referenced before assignment" or what is the correct way to declare a variable?

There is a piece of code:

ACTIVE_WINDOW = ''
def on_press(key):
  f = open('text.txt', 'a')
  if ACTIVE_WINDOW != GetWindowText(GetForegroundWindow()):
    ACTIVE_WINDOW = GetWindowText(GetForegroundWindow())
    f.write(ACTIVE_WINDOW)
    print(ACTIVE_WINDOW)

Gives an error message:
UnboundLocalError: local variable 'ACTIVE_WINDOW' referenced before assignment

I realized that when comparing, it does not see the variable, but no matter how I tried to declare it, it still gives exactly the same error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-05-15
@hesy

Here it is - the result of unsystematic training! Nobody reads textbooks.

active_window = ''

def on_press(key):
    global active_window
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question