Jay
Jay

Reputation: 291

Python local variable referenced before assignment

I have created some code:

 import numpy as np
 Length=(2.7)*10**-3
 Nx=4
 x = np.linspace(0, Length, Nx+1)   # mesh points in space
 t1=110
 t2=100
 m=((t2-t1)/Length)
 T=5
 N=5        
 t = np.linspace(0, T, N+1)
 Coeff=0.5
 b=0.2
 tamb = 20
 u = np.zeros(Nx+1)
 u_1 = np.zeros(Nx+1)
 for i in range(0, Nx+1):
   u_1[i] = m*(x[i])+t1
 #print u_1
 r=[]
 for n in range(0, N+1):
 # Compute u at inner mesh points
 for i in range(0,1):
    u[i] = 2*Coeff*(u_1[i+1]+b*tamb)+(1-2*Coeff-2*b*Coeff)*u_1[i]
 for i in range(1,Nx):
    u[i] = Coeff*(u_1[i+1]+u_1[i-1])+(1-2*Coeff)*u_1[i]
 for i in range(Nx,Nx+1):
    u[i] = 2*Coeff*(u_1[i-1])+(1-2*Coeff)*u_1[i]
 # Switch variables before next step
 u_1, u = u, u_1
 r.append(u.copy()) 
 print r[5]

Output for code:

 [  78.1562   94.1595   96.82    102.6375  102.125 ]

Using the code I have created a function to apply to an array:

def function(data,time):
  import numpy as np
  Values=data[n]
  Length=(Values[2])*10**-3
  Nx=4
  x = np.linspace(0, Length, Nx+1)   # mesh points in space
  t1=Values[0]
  t2=Values[1]
  m=((t2-t1)/Length)
  T=time[5]
  N=5        
  t = np.linspace(0, T, N+1)
  Coeff=0.5
  b=0.2
  tamb = 20
  u = np.zeros(Nx+1)
  u_1 = np.zeros(Nx+1)
  for i in range(0, Nx+1):
      u_1[i] = m*(x[i])+t1
    #print u_1
  r=[]
  for n in range(0, N+1):
  # Compute u at inner mesh points
      for i in range(0,1):
        u[i] = 2*Coeff*(u_1[i+1]+b*tamb)+(1-2*Coeff-2*b*Coeff)*u_1[i]
      for i in range(1,Nx):
        u[i] = Coeff*(u_1[i+1]+u_1[i-1])+(1-2*Coeff)*u_1[i]
      for i in range(Nx,Nx+1):
        u[i] = 2*Coeff*(u_1[i-1])+(1-2*Coeff)*u_1[i]
  # Switch variables before next step
      u_1, u = u, u_1
      r.append(u.copy()) 
  return r
import numpy as np

#arrays
data=np.array(((110,100,2.5),(112,105,2.6),(115,109,2.7)))
time=np.array((0,1,2,3,4,5))
#apply function to array
for n in range(len(data)):
   r = function(data,time)
   print r[5]

The 1st code works fine but when I apply the code using a function (2nd Code) if I tell I get the following error:

Traceback (most recent call last):
File "C:/Users/a/Desktop/functiontrial3.py", line 39, in <module>
r = function(data,time)
File "C:/Users/a/Desktop/functiontrial3.py", line 3, in function
Values=data[n]
UnboundLocalError: local variable 'n' referenced before assignment

What do I have to do to get the following code to work?

Upvotes: 1

Views: 5236

Answers (3)

iun1x
iun1x

Reputation: 1093

You are trying to use a variable n in your defined function which is local to the loop block for n in range(0, N+1): , As the function header def function(data,time): doesn't contain any argument parameter referring to n, so the code doesn't reach the point where n gets a value as it is not global (and by that, it gets undefined/unbound). Depending on how you want to solve the problem you should set the variable in place so that it can get executed for instance.

def function(data, time, n):#n passed as positional argument
    Values=data[n]
    --snip--

or

def function(data, time, n):#n passed as positional argument
    for n in range(0, N+1): 
        # Compute u at inner mesh points
        # As per your problem, the rest will depend on you target...
        Values=data[n]
        --snip--

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304463

You're using the global n here

Values=data[n]

You're using n as a local variable here

for n in range(0, N+1):

Python won't let you use n as both a global and local in the same scope.

Is it supposed to be the same n or is it just a bad reuse of a variable name?

There are several ways to fix this error, but it depends on your intent.

Upvotes: 3

perreal
perreal

Reputation: 98118

Change your function signature:

def function(data,time,n):

and call it like this:

for n in xrange(len(data)):
   r = function(data,time,n)

Upvotes: 1

Related Questions