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

Heat Index表格生成异常:Python代码输出与预期结果不符

热指数表格代码问题排查与修复

问题描述

编写了生成热指数(Heat Index)表格的Python代码,表格的列、行表头及第一列内容正确,但其余表格内容异常——每行的热指数值均为相同数值,与预期的差异化热指数表格结果不符。

错误原因

核心问题出在表格数值生成的内层循环中:

  1. 调用calculate_heat_index时,温度参数固定传了first_value(即70),没有随列变化计算对应温度;
  2. 错误地在循环中对value2(当前行的相对湿度)进行累加step操作,导致后续计算的相对湿度完全偏离当前行的设定值。

修正后的代码

# 计算热指数的函数
def calculate_heat_index(temperature, relative_humidity):
    c1 = -42.379
    c2 = 2.04901523
    c3 = 10.14333127
    c4 = -0.22475541
    c5 = -6.83783e-03
    c6 = -5.481717e-02
    c7 = 1.22874e-03
    c8 = 8.5282e-04
    c9 = -1.99e-06

    heat_index = (
        c1
        + c2 * temperature
        + c3 * relative_humidity
        + c4 * temperature * relative_humidity
        + c5 * temperature ** 2
        + c6 * relative_humidity ** 2
        + c7 * temperature ** 2 * relative_humidity
        + c8 * temperature * relative_humidity ** 2
        + c9 * temperature ** 2 * relative_humidity ** 2
    )

    return heat_index

first_value = 70
last_value = 90
num_values = 8
step = (last_value - first_value) / (num_values - 1)
second_value = 50
boo_value = 85
num2_values = 10
step2 = (boo_value - second_value) / (num2_values - 1)

# 打印表格表头
print("Heat Index")
print(" RH \ T ", end="\t")
for t in range(8):
    value = first_value + t * step
    print(f"{value:.1f}   ", end="\t")
print("\n -------", end="\t")

# 打印分隔线
for _ in range(8):
    print("-----", end="\t")
print()
print()

# 打印表格内容
for x in range(10):
    value2 = second_value + x * step2
    print(f"  {value2:.1f}   ", end="\t")
    for temp in range(8):
        # 计算当前列对应的温度,使用当前行的相对湿度
        current_temp = first_value + temp * step
        heat_index = calculate_heat_index(current_temp, value2)
        print(f"{heat_index:.1f}", end="\t")
    print()

修正后效果

运行修正后的代码,将生成符合预期的差异化热指数表格,每行的热指数会随温度列变化,与预期输出一致:

Heat Index
 RH \ T     70.0    72.9    75.7    78.6    81.4    84.3    87.1    90.0       
 -------    -----   -----   -----   -----   -----   -----   -----   -----   

  50.0      76.9    77.0    77.9    79.6    82.2    85.5    89.6    94.6    
  53.9      76.6    76.8    77.9    79.9    82.7    86.4    91.0    96.4    
  57.8      76.2    76.6    77.9    80.1    83.3    87.4    92.5    98.4    
  61.7      75.6    76.2    77.8    80.3    83.9    88.5    94.1    100.6    
  65.6      74.9    75.7    77.6    80.6    84.6    89.7    95.8    103.0    
  69.4      74.0    75.1    77.4    80.8    85.3    90.9    97.7    105.5    
  73.3      73.0    74.5    77.1    81.0    86.0    92.3    99.7    108.3    
  77.2      71.8    73.7    76.8    81.2    86.8    93.7    101.8    111.2    
  81.1      70.5    72.8    76.5    81.4    87.7    95.2    104.1    114.2    
  85.0      69.0    71.9    76.1    81.6    88.6    96.8    106.5    117.5

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 17:13:15