Linked Questions

392 votes
5 answers
1.5m views

The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment: Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("...
Eden Crow's user avatar
  • 16.4k
231 votes
8 answers
334k views

What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
165 votes
3 answers
494k views

test1 = 0 def test_func(): test1 += 1 test_func() I am receiving the following error: UnboundLocalError: local variable 'test1' referenced before assignment. Error says that 'test1' is local ...
foxneSs's user avatar
  • 2,279
84 votes
5 answers
419k views

I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and ...
39 votes
4 answers
64k views

How do global variables work in Python? I know global variables are evil, I'm just experimenting. This does not work in python: G = None def foo(): if G is None: G = 1 foo() I get an ...
user avatar
77 votes
3 answers
580k views

I don't know why when a is located in def test() it can not be found and gives the error UnboundLocalError: cannot access local variable 'a' where it is not associated with a value Code import ...
Oliver Sklar's user avatar
52 votes
3 answers
64k views

In the following Python code, I get an UnboundLocalError. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I ...
41 votes
4 answers
20k views

I am trying to figure out this: c = 1 def f(n): print c + n def g(n): c = c + n f(1) # => 2 g(1) # => UnboundLocalError: local variable 'c' referenced before assignment Thanks!
33 votes
8 answers
55k views

I'm trying to add or subtract from a defined variable, but how can I overwrite the old value with the new one? a = 15 def test(): a = a + 10 print(a) test() Error message: Traceback (most ...
33 votes
7 answers
8k views

If I run the following code: x = 1 class Incr: print(x) x = x + 1 print(x) print(x) It prints: 1 2 1 Okay no problems, that's exactly what I expected. And if I do the following: x = 1 ...
Shashank's user avatar
  • 13.9k
34 votes
2 answers
38k views

From the Google Style Guide on lexical scoping: A nested Python function can refer to variables defined in enclosing functions, but can not assign to them. Both of these seem to check out at first:...
19 votes
3 answers
4k views

Last Friday I went to a job interview and had to answer the following question: why does this code raise an exception (UnboundLocalError: local variable 'var' referenced before assignment on the line ...
oopcode's user avatar
  • 1,972
2 votes
5 answers
79k views

The answer should be 2 because first the main() function is called, then the first() function is called, overriding the global variable num = 0 which was defined outside of any functions, therefore ...
Dr.Doom2020's user avatar
1 vote
2 answers
62k views

Ive got a method that use to work by checking the first three letters/numbers and making sure they are the same before it continues like so def combineProcess(request): carID1 = request.POST['...
11 votes
1 answer
43k views

I cannot understand what is the problem in my Python code. It gives me the following error: Traceback (most recent call last): File "main.py", line 77, in <module> main(); File "...
YohanRoth's user avatar
  • 3,263

15 30 50 per page
1
2 3 4 5
...
31