python - 如何修复代码中的 'UnboundLocalError: local variable ' dc' 在分配之前引用'?

标签 python class variables user-defined-functions

在调用 dc 之前,我无法找到确切的引用位置。任何帮助,将不胜感激。代码取自https://cfs.nrcan.gc.ca/pubwarehouse/pdfs/36461.pdf第14页但手册中的代码有一些缩进错误。相关代码(据我所知):

    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

我的错误:

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

最后的 main() 函数:

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。

关于python - 如何修复代码中的 'UnboundLocalError: local variable ' dc' 在分配之前引用'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025748/

相关文章:

python - 传递列表中第一个更改的值

python - Postgres SQLAlchemy 自动递增不起作用

php - 在 Zend 中使用 BaseController 扩展 IndexController

python - 如何检查变量是否存在?

python - Windows 上缺少 `magic` 库的文件

python - 如何使用 python 在绘图中使用交互式单击事件打开文件

javascript - 类中的异步/等待 : unexpected token `this`

css - 如何使用 SASS 定义 css 选择器类前缀

php - 使用 PHP PDO 查询中传递的变量

javascript - 在函数执行之间保留变量 (Javascript)