You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python报错NameError: name 'thetas' is not defined的原因排查

排查NameError: name 'thetas' is not defined错误原因

直接错误原因

第12行代码thetas = np.append(thetas, range(0,35,5))中,np.append的第一个参数要求传入已存在的数组,但此时thetas还未被定义或初始化,Python无法找到该变量,因此抛出NameError。

修复方案

有两种简洁的修复方式:

  1. 先初始化空数组再追加元素:
thetas = np.array([])
thetas = np.append(thetas, range(0,35,5))
  1. 直接生成目标数组(更高效简洁):
thetas = np.arange(0, 35, 5)

额外潜在问题

原代码中存在一处缩进错误:

for k in range(thetas.size):
    # ... 循环内其他代码
Abs[:,k] = np.asarray([(1-Refl[j,k])*math.cos(math.radians(theta_out[j,k])) for j in range(50)])

Abs[:,k]的赋值语句未缩进,不在for循环代码块内,会导致只有最后一个k对应的列被计算,其余列保留初始空值,需将这行代码缩进,放入for循环内部。

内容的提问来源于stack exchange,提问作者Quant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 13:43:16