对象是不可迭代的。我如何使此代码起作用

时间:2018-10-01 18:31:41

标签: python-3.x

有人可以帮助我解决此问题,为什么它返回此错误: 对象是不可迭代的。

def leap_year(start_year, end_year):
    for start_year in end_year:
        # Leap Year Check
        if start_year % 4 == 0 and start_year % 100 != 0:
            print(start_year, "is a Leap Year")
        elif start_year % 100 == 0:
            print(start_year, "is not a Leap Year")
        elif start_year % 400 ==0:
            print(start_year, "is a Leap Year")
        else:
            print(start_year, "is not a Leap Year")
        if end_year % 4 == 0 and end_year % 100 != 0:
            print(end_year, "is a Leap Year")
        elif end_year % 100 == 0:
            print(end_year, "is not a Leap Year")
        elif end_year % 400 ==0:
            print(end_year, "is a Leap Year")
        else:
            print(end_year, "is not a Leap Year")

leap_year(2000, 2006)

1 个答案:

答案 0 :(得分:0)

您输入的end_year是2006,这是一个不可迭代的数字。

如果要遍历end_year,则需要将其设为

for start_year in end_year

要使end_year变得可迭代,您可以尝试range(end_year)

for start_year in range(end_year)

将留给您查找范围功能