When I try this code:
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 an error that says
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
works correctly but the second causes an exception. I tried making T a global variable but then both answers are the same.
What is going wrong, and how can I fix it?