简体   繁体   English

For循环没有完全迭代

[英]For loop not iterating completely through

I am working on a project that consists of calling an API and returning per diem rates that are given when on company travel.我正在开展一个项目,该项目包括调用 API 并返回公司旅行时提供的每日津贴。 Since travel rates change by month, I must account for if the travel goes across months.由于旅行费用按月变化,我必须考虑旅行是否跨越几个月。 When this occurs, I am compiling the rates into an array and then returning that.发生这种情况时,我将费率编译成一个数组,然后返回它。 However, whenever I run the for loop that produces the array, it only returns the first value.但是,每当我运行生成数组的 for 循环时,它只返回第一个值。 Any help is greatly appreciated: I have the section of my code below:非常感谢任何帮助:我的代码部分如下:

    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 per_diem_rates
    else:
        return per_diem_rate

The dates I am querying span from Sep to Nov, which means I should be getting three different rates for each month.我查询的日期从 9 月到 11 月,这意味着我每个月应该得到三种不同的费率。 Thank you!谢谢!

Your indentation is wrong.你的缩进是错误的。 The function will return after the first iteration in the loop. function 将在循环中的第一次迭代后返回。 Change it like that:像这样改变它:

        for i in months:
            rate = r["rates"][0]["rate"][0]["months"]["month"][i - 1]["value"]
            per_diem_rates.append(rate)
        return per_diem_rates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM