"UnboundLocalError: local variable referenced before assignment" after an if statement

I have also tried searching for the answer but I don't understand the answers to other people's similar problems...

tfile= open("/home/path/to/file",'r') 

def temp_sky(lreq, breq):
    for line in tfile:
        data = line.split()
        if (    abs(float(data[0]) - lreq) <= 0.1 
            and abs(float(data[1]) - breq) <= 0.1):            
            T= data[2]
    return T
print temp_sky(60, 60)
print temp_sky(10, -10)

I get the following error

7.37052488
Traceback (most recent call last):
File "tsky.py", line 25, in <module>
  print temp_sky(10, -10)
File "tsky.py", line 22, in temp_sky
  return T
UnboundLocalError: local variable 'T' referenced before assignment

The first print statement works correctly but it won't work for the second. I have tried making T a global variable but this makes both answers the same! Please help!


Your if statement is always false and T gets initialized only if a condition is met, so the code doesn't reach the point where T gets a value (and by that, gets defined/bound). You should introduce the variable in a place that always gets executed.

Try:

def temp_sky(lreq, breq):
    T = <some_default_value> # None is often a good pick
    for line in tfile:
        data = line.split()
        if abs(float(data[0])-lreq) <= 0.1 and abs(float(data[1])-breq) <= 0.1:
            T = data[2]
    return T

FWIW: I got the same error for a different reason. I post the answer here not for the benefit of the OP, but for the benefit of those who may end up on this page due to its title... who might have made the same mistake I did.

I was confused why I was getting "local variable referenced before assignment" because I was calling a FUNCTION that I knew was already defined:

def job_fn(job):
  return job + ".job"

def do_something():
  a = 1
  b = 2
  job_fn = job_fn("foo")

do_something()

This was giving:

UnboundLocalError: local variable 'job_fn' referenced before assignment

Took me a while to see my obvious problem: I used a local variable named job_fn which masked the ability to see the prior function definition for job_fn.


The other answers are correct: You don't have a default value. However, you have another problem in your logic:

You read the same file twice. After reading it once, the cursor is at the end of the file, so trying to read it again returns nothing and the loop is never entered. To solve this, you can do two things: Either open/close the file upon each function call:

def temp_sky(lreq, breq):
    with open("/home/path/to/file",'r') as tfile:
        # do your stuff

This has the disadvantage of having to open the file each time. The better way would be:

tfile.seek(0)

You do this after your for line in tfile: loop. It resets the cursor to the beginning so the next call will start from there again.

Related questions:

  • Iterating on a file doesn't work the second time
  • Why a file is empty after reading it?