Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variable referenced before assignment in Python

Truel=""
count = 0
finle_touch=False #true after it find the first 3 upperletter

# check if there is 1 lower letter after three upper letter
def one_lower(i):
    count=0
    if i == i.lower:
        finle_touch=True
        Truel=i

# check for 3 upper letter
def three_upper(s):
    for i in s:
        if count == 3:
            if finle_touch==True:
                break
            else:
                one_lower(i)
        elif i == i.upper:
            count +=1
            print(count) #for debug
        else:
            count ==0
            finle_touch=False

stuff="dsfsfFSfsssfSFSFFSsfssSSsSSSS......."
three_upper(stuff)
print(Truel)

So I have a lot of string on 'stuff' and I like to find 1 lowercase letter that's surrounded by 3 uppercase letter.

But when I run this code I get:

Traceback (most recent call last):
  File "C:\Python33\mypy\code.py", line 1294, in <module>
    three_upper(stuff)
  File "C:\Python33\mypy\code.py", line 1280, in three_upper
    if count == 3:
UnboundLocalError: local variable 'count' referenced before assignment

I don't understand why.

like image 520
Or Halimi Avatar asked Jul 06 '13 20:07

Or Halimi


People also ask

How do I fix local variable referenced before assignment?

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function. To solve the error, mark the variable as global in the function definition, e.g. global my_var .

What does local variable referenced before assignment?

The local variable referenced before assignment occurs when some variable is referenced before assignment within a function's body. The error usually occurs when the code is trying to access the global variable.

How do I fix UnboundLocalError local variables?

UnboundLocalError can be solved by changing the scope of the variable which is complaining. You need to explicitly declare the variable global. Variable x's scope in function printx is global. You can verify the same by printing the value of x in terminal and it will be 6.

What is an unbound local error in Python?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.


2 Answers

Due to this line count +=1 python thinks that count is a local variable and will not search the global scope when you used if count == 3:. That's why you got that error.

Use global statement to handle that:

def three_upper(s): #check for 3 upper letter     global count     for i in s: 

From docs:

All variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

like image 186
Ashwini Chaudhary Avatar answered Sep 21 '22 22:09

Ashwini Chaudhary


It is actually better to use nonlocal in this case. Use global as sparingly as possible. More information about nonlocal here docs.python.org/3/reference/simple_stmts.html#nonlocal

like image 34
Colin Schoen Avatar answered Sep 24 '22 22:09

Colin Schoen