【发布时间】:2021-11-17 00:17:32
【问题描述】:
我正在对能耗进行一些数据分析,并在文件 data_inputs.py 中编写了以下函数以提供帮助。
elec_kWh = 50000
gas_MJ = 0
coal_kg = 0
oil_L = 0
#%% sum energy consumption
def elec_MJ(elec_kWh):
elec_MJ = (elec_kWh * 3.6)
return elec_MJ
def coal_MJ(coal_kg):
coal_MJ = (coal_kg * 22.1)
return coal_MJ
def oil_MJ(oil_L):
oil_MJ = (oil_L * 38.6)
return oil_MJ
#%% sum energy consumption
def total_energy_consumption_MJ(elec_kWh, gas_MJ, coal_kg, oil_L):
total_consumption = (elec_MJ(elec_kWh)+
gas_MJ +
coal_MJ(coal_kg) +
oil_MJ (oil_L)
)
return total_consumption
print (total_energy_consumption_MJ(elec_kWh, gas_MJ, coal_kg, oil_L))
def elec_percent_cons(
elec_kWh,
gas_MJ,
coal_kg,
oil_L
):
elec_percent_cons = (
elec_MJ(elec_kWh) /
total_energy_consumption_MJ(elec_kWh, gas_MJ, coal_kg, oil_L)
)
return elec_percent_cons
我希望将这些数据输入函数导入到具有更复杂数据建模的数据输出文件中。
当我在 data_inputs.py 中运行这个函数时,它运行良好。但是当我尝试将 elec_percent_cons 导入 data_outputs.py 文件时,出现以下错误:
from data_inputs import elec_kWh, gas_MJ, coal_kg, oil_L, total_energy_consumption_MJ, elec_percent_cons
12
13 def elec_MJ(elec_kWh):
---> 14 elec_MJ = elec_kWh * 3.6
15 return elec_MJ
16
TypeError: unsupported operand type(s) for *: 'function' and 'float'
我很困惑这是如何发生的——我已经确认该函数在 data_inputs.py 中打印了一个值,并将 elec_kWh 的浮点值导入到函数中。我不明白它如何将 elec_kWh 值解释为函数?
【问题讨论】:
-
您将函数传递给
elec_MJ,而不是数字。 -
^ 同意。我正在努力查看错误发生在哪里。将您的函数分配给与函数同名的变量并没有帮助,这在眼睛上有点令人困惑。我会像“return elec_kWh * 3.6”一样直接返回
-
实际上,这段代码对我有用。我刚刚测试了将其导入文件并且可以正常工作。一定有其他的东西没有在这里分享,这对你不利。