Skip to content
Advertisement

Local variable referenced before assignment python error

I have this code which needs to be plotted perfectly. I’m new to python. The error is as mentioned above. Any kind of help would be appreciated.

    #Variable Initialization
    #%pylab inline
    # (a) Curie Temperature for h = 15 W/m^2
    # (b) Value of h for cure temp = 50 deg C

    import math
    import numpy
    from numpy import roots
    import matplotlib
    from matplotlib import pyplot
    Tsurr = 30+273;                                     #[K] - Temperature of surrounding
    Tf = 20+273;                                        #[K] - Temperature of Fluid Flow
    e=.5;                                               #  Emissivity of Surface
    a = .8;                                             # Absorptivity of Surface
    G = 2000;                                           #[W/m^2] - Irradiation falling on surface
    h=15;                                               #[W/m^2.k] - Thermal Convectivity from plate to air
    stfncnstt=5.67*math.pow(10,(-8));                   # [W/m^2.K^4] - Stefan Boltzmann Constant 
    T=375;                                              #[K] Value initially assumed for trial-error approach
    #Using Eq 1.3a & 1.7 and trial-and error approach of Newton Raphson 
    #calculations and results
    while(1>0):
        f=((a*G)-(h*(T-Tf)+e*stfncnstt*(T*T*T*T - Tsurr*Tsurr*Tsurr*Tsurr)));
        fd=(-h*T-4*e*stfncnstt*T*T*T);
        Tn=T-f/fd;
        if(((a*G)-(h*(Tn-Tf)+e*stfncnstt*(Tn*Tn*Tn*Tn - Tsurr*Tsurr*Tsurr*Tsurr)))<.01):
            break;
        T=Tn;

    print '%s %.2f %s' %("n (a) Cure Temperature of Plate =",T-273.,"degCn");
    #solution (b)
    Treq=50+273;
    #def T(h):
    #    t=375;
    #    while(1>0):
    #        f=((a*G)-(h*(t-Tf)+e*stfncnstt*(t*t*t*t - Tsurr*Tsurr*Tsurr*Tsurr)));
    #        fd=(-h*t-4*e*stfncnstt*t*t*t);
    #        Tn=t-f/fd;
    #        if((a*G)-(h*(Tn-Tf)+e*stfncnstt*(Tn*Tn*Tn*Tn - Tsurr*Tsurr*Tsurr*Tsurr))<.01):
    #           break;
    #    tnew=Tn;
    #    return tnew;
    rot=numpy.zeros(4);
    rt=0;

    def T(h):
        coeff = ([-e*stfncnstt, 0,0, -h, a*G+h*Tf+e*stfncnstt*Tsurr*Tsurr*Tsurr*Tsurr]);
        rot=numpy.roots(coeff);
        for i in range (0,3):
            if rot[i]<250 and rot[i]>0:
                rt=rot[i];
        return rt;
Error at this part. The function is not working.! What can be the error. Please suggest.     
    h = range(0,100)
    tn=range(0,100)
    for i in range (0,100):
        tn[i] = T(i) -273;

    Ti=50+273;
    hnew=((a*G)-(e*stfncnstt*(Ti*Ti*Ti*Ti - Tsurr*Tsurr*Tsurr*Tsurr)))/(Ti-Tf);

    pyplot.plot(h,tn);
    pyplot.xlabel("h (W m^2/K)");
    pyplot.ylabel("T (C)");
    pyplot.show();
    print '%s %.2f %s' %("n (b) Air flow must provide a convection of =",hnew," W/m^2.K");
    #print '%s' %("n The code for the graph requires more than 10 min to run. ")
    #print '%s' %("n To run it, please remove comments. It is perfectly correct. The reason it takes such a long time")
    #print '%s' %("n  is that it needs to calculate using Newton raphson method at 100 points. Each point itself takes a minute.")
    #END

Advertisement

Answer

it will say

UnboundLocalError: local variable 'rt' referenced before assignment

since you have assigned something to rt rt=rot[i], rt is recognized as a local variable then. If you want to make it global, make it global explicitly using global rt:

def T(h):
    global rt
    #do sth....

note that even if the assignment is not executed, the intepreter will still see assigned var as local:

In [136]: var=2
     ...: def foo():
     ...:   if False:
     ...:       var=4
     ...:   print var
     ...:  

In [137]: foo()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-137-624891b0d01a> in <module>()
----> 1 foo()

<ipython-input-136-ab29dd655074> in foo()
      3         if False:
      4                 var=4
----> 5         print var
      6 

UnboundLocalError: local variable 'var' referenced before assignment
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement