简体   繁体   中英

How do I fix 'UnboundLocalError: local variable 'dc' referenced before assignment' in my code?

I'm having trouble finding where exactly dc is being referenced before it's called. Any help would be appreciated. Code is taken from page 14 of https://cfs.nrcan.gc.ca/pubwarehouse/pdfs/36461.pdf but the code in the manual has some indentation errors. Relevant code (as far as I know):

    def DCcalc(self,dc0,mth):
        fl = [-1.6, -1.6, -1.6, 0.9, 3.8, 5.8, 6.4, 5.0, 2.4, 0.4, -1.6, -1.6]
        t = self.t
        if(t < -2.8):
            t = -2.8
        pe = (0.36*(t+2.8) + fl[mth-1] )/2
        if pe <= 0.0:
            pe = 0.0
        if (self.p > 2.8):
            ra = self.p
            rw = 0.83*ra - 1.27 #*Eq. 18*#
            smi = 800.0 * math.exp(-dc0/400.0) #*Eq. 19*#
            dr = dc0 - 400.0*math.log( 1.0+((3.937*rw)/smi) ) #*Eqs. 20 and 21*#
            if (dr > 0.0):
                dc = dr + pe
        elif self.p <= 2.8:
            dc = dc0 + pe
        return dc

And my error:

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-c2d03971ed28> in <module>
    140         infile.close()
    141         outfile.close()
--> 142 main()

<ipython-input-3-c2d03971ed28> in main()
    129             ffmc = round(fwisystem.FFMCcalc(ffmc0),2)
    130             dmc = round(fwisystem.DMCcalc(dmc0,mth),2)
--> 131             dc = round(fwisystem.DCcalc(dc0,mth),2)
    132             isi = round(fwisystem.ISIcalc(ffmc),2)
    133             bui = round(fwisystem.BUIcalc(dmc,dc),2)

<ipython-input-3-c2d03971ed28> in DCcalc(self, dc0, mth)
     87         elif self.p <= 2.8:
     88             dc = dc0 + pe
---> 89         return dc
     90     def ISIcalc(self,ffmc):
     91         mo = 147.2*(101.0-ffmc) / (59.5+ffmc)

UnboundLocalError: local variable 'dc' referenced before assignment

And the main() function at the very end:

def main():
    ffmc0 = 85.0
    dmc0 = 6.0
    dc0 = 15.0
    infile = open('c:Documents/data.txt','r')
    outfile = open('fwioutput.txt','w')
    try:
        for line in infile:
            mth,day,temp,rhum,wind,prcp,year=[float(field) for field in 
                                         line.strip().lstrip('[').rstrip(']').split()]
            if rhum>100.0:
                rhum = 100.0
            mth = int(mth)
            fwisystem = FWICLASS(temp,rhum,wind,prcp)
            ffmc = round(fwisystem.FFMCcalc(ffmc0),2)
            dmc = round(fwisystem.DMCcalc(dmc0,mth),2)
            dc = round(fwisystem.DCcalc(dc0,mth),2)
            isi = round(fwisystem.ISIcalc(ffmc),2)
            bui = round(fwisystem.BUIcalc(dmc,dc),2)
            fwi = round(fwisystem.FWIcalc(isi,bui),2)
            ffmc0 = ffmc
            dmc0 = dmc
            dc0 = dc
            outfile.write("%s %s %s %s %s %s\n"%(str(ffmc),str(dmc),str(dc),str(isi),str(bui),str(fwi)))
    finally:
        infile.close()
        outfile.close()
main()

考虑 self.p > 2.8 和 dr <= 0.0 这使得 dc 从未被分配过任何东西,并使该方法返回甚至不存在的 dc 。

Consider self.p < 2.8. dc is then never defined before the return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM