首页 > 解决方案 > 到达一个无法到达的 ELSE

问题描述

我在这个函数中包含了一个无法到达的条件。问题是它刚刚到达。我不知道如何解决它。

def bcirrus_func1(Qn):
    if Qn <= -1:
        bcirrus = 0
    elif Qn > -1 and Qn <= 0:
        bcirrus = 0.5*(1-Qn)**2
    elif Qn > 0 and Qn < 1:
        bcirrus = 1 - 0.5*(1-Qn)**2
    elif Qn >= 1:
        bcirrus = 1
    else:
        print('Something has gone very wrong')
    return(bcirrus)

怎么会触发“发生了很大的问题”?

这是错误:

/.local/lib/python3.6/site-packages/pint/numpy_func.py:289: RuntimeWarning: overflow encountered in exp
  result_magnitude = func(*stripped_args, **stripped_kwargs)
/.local/lib/python3.6/site-packages/pint/quantity.py:1160: RuntimeWarning: invalid value encountered in double_scalars
  magnitude = magnitude_op(new_self._magnitude, other._magnitude)
Something has gone very wrong
Traceback (most recent call last):
  File "./make_pcc_layer.py", line 122, in <module>
    pcc1, pcc2 = cc.PCC(layer_pressure,layer[i][j].tmp-273.15,layer[i][j].rh,layer[i][j].icmr)
  File "//weather_box/earth/clouds_and_contrails.py", line 119, in PCC
    PCC1 = bcirrus_func1(Qnstar)-bcirrus_func1(Qn)
  File "//weather_box/earth/clouds_and_contrails.py", line 39, in bcirrus_func1
    return(bcirrus)
UnboundLocalError: local variable 'bcirrus' referenced before assignment

编辑:我将 Qn 添加到“无法访问”的打印语句中,正如建议的那样,它是 NaN。这是输出:

Something has gone very wrong: Qn nan dimensionless

它说“无量纲”,因为它使用的是品脱。

标签: python

解决方案


Qn在这里疯狂猜测,但有可能nan吗?如果是这样,它有奇怪的(阅读:可能是直观的)比较行为

>>> import math
>>> x = math.nan
>>> x < -1
False
>>> x > 1
False

推荐阅读