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

Python循环未完整遍历:跨月度Per Diem Rates API返回值异常排查

问题排查与修复

核心问题出在return语句的位置:你把return per_diem_rates写在了for循环内部,导致循环执行第一次迭代(处理9月)时就直接返回仅包含第一个值的数组,后续月份的循环根本没机会执行。

修复后的代码

if month1 != month2:
    per_diem_rates = []
    if int(month2) < int(month1):
        months = list(range(int(month1), 13))
        months2 = list(range(1, int(month2) + 1))
        months += months2
    else:
        months = list(range(int(month1), (int(month2) + 1)))

    for i in months:
        rate = r["rates"][0]["rate"][0]["months"]["month"][i - 1]["value"]
        per_diem_rates.append(rate)
    # 将return移到循环外部,确保所有月份处理完成后再返回
    return per_diem_rates
else:
    return per_diem_rate

说明

把return per_diem_rates移出循环后,程序会遍历months列表中的所有目标月份(比如9、10、11月),将每个月份的补贴值依次加入数组,最后一次性返回完整的结果数组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:35:16