python - 到达一个无法到达的 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
它说“无量纲”,因为它使用的是品脱。
解决方案
Qn
在这里疯狂猜测,但有可能nan
吗?如果是这样,它有奇怪的(阅读:可能是直观的)比较行为
>>> import math
>>> x = math.nan
>>> x < -1
False
>>> x > 1
False
推荐阅读
- python - 使用 pycountry 从国家/地区获取大陆名称
- javascript - 使用 Vue.js 构建评论系统时,Laravel 5.8 Vue is not defined 错误
- swift - 如何快速将数字附加到双精度
- scala - 如何引用生成的源文件?
- swift - 如何使用 loadHTMLString() 实现 WKWebView History 的滑动手势
- c# - Angular 7多个查询字符串,如何
- javascript - 从 java 中获取 javascript 变量的属性
- typescript - 打字稿创建具有所有关键属性但类型不同的对象副本
- java - 从类中调用方法时遇到问题
- scala - 我应该使用对象类还是广播变量