Python for循环,范围计数器

时间:2018-05-24 19:40:20

标签: python for-loop counter

我制作了一个打印每年日期的程序。是否可以添加一个允许限制可打印日期的计数器?我会每隔7天打印一次。谢谢你的建议。 以下原始代码。

def main():

    for month in range(1, 13):
        daymax = 32
        if month == 2:
            daymax = 29
        elif month == 4:
            daymax = 31
        elif month == 6:
            daymax = 31
        elif month == 9:
            daymax = 31
        elif month == 11:
            daymax = 31

        for day in range(1, daymax):
            print(day,".", month,".", sep="")


main()

2 个答案:

答案 0 :(得分:0)

只需使用另一个计数器变量,并检查它是否是7的倍数。

def main():
    counter = 0
    for month in range(1, 13):
        daymax = 32
        if month == 2:
            daymax = 29
        elif month == 4:
            daymax = 31
        elif month == 6:
            daymax = 31
        elif month == 9:
            daymax = 31
        elif month == 11:
            daymax = 31

        for day in range(1, daymax):
            if counter % 7 == 0:
                print(day,".", month,".", sep="")
            counter += 1

答案 1 :(得分:0)

ERROR_PROVIDER_ALREADY_LINKED

应该有用。试试吧。