交作业 日历

def is_leap_year(year):
    #判断闰年
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        return True
    else:
        return False


def get_month_days(year, month):
    #判断哪一年哪一月有几天
    days = 31
    if month == 2:
        days = 29 if is_leap_year(year) else 28
    elif month in [4, 6, 9, 11]:
        days = 30
    return days


def get_today(year, month):
    #判断到之前共有多少天
    total = 0
    for i in range(1990, year):
        if is_leap_year(year):
            total += 366
        else:
            total += 365
    for i in range(1, month):
        total += get_month_days(year, i)
    return total

if __name__ == '__main__':

    while True:
        year = input('请输入年份, (如: 1990): ')
        month = input('请输入月份: (如:1): ')
        try:
            year = int(year)
            month = int(month)
            if month < 1 or month > 12:
                print("年份或月份输入错误,请重新输入!")
                continue
        except:
            print("年份或月份输入错误,请重新输入!")
            continue
        break

    print("日\t一\t二\t三\t四\t五\t六")
    count = 0
    for i in range((get_today(year, month) % 7) + 1):
        print("\t", end="")
        count += 1
    for i in range(1, (get_month_days(year, month)+1)):
        print(i, end="")
        print("\t", end="")
        count += 1
        if count % 7 == 0:
            print("\n")
交作业 日历_第1张图片
image.png

你可能感兴趣的:(交作业 日历)