万年历Python的入门编写

#万年历

#根据年份判断是否是闰年
#四年一闰,百年不闰,四百年再闰.
#rYear = inYear%400==0 or (inYear%4==0 and inYear%100!=0)
list31 = [1,3,5,7,8,10,12]
list30 = [4,6,9,11]
#月的天数:闰年二月是29天,平年二月是28天
dayM1 = 31
dayM2 = 30
dayM3 = 29
dayM4 = 28
#年的天数:闰年366天,平年365天
dayY1 = 365
dayY2 = 366
#判断每个月份的天数
def monthDay(inYear,inMonth):
    if inMonth in list31:
         return dayM1
    elif inMonth in list30:
        return dayM2
    else:
        if inYear%400==0 or (inYear%4==0 and inYear%100!=0):
            return dayM3
        else:
            return dayM4
#判断年份的天数
def yearDay(inYear):
    if inYear%400==0 or (inYear%4==0 and inYear%100!=0):
        return dayY2
    else:
        return dayY1
#输入年份到1900年的总天数
def allYearDay(inYear):
    allDay = 0
    for allYear in range(1900,inYear):
        allDay += yearDay(allYear)
    return allDay
#输入年份的月份到本年的1-1总天数
def allMonthDay(inYear,inMonth):
    allDay = 0
    for allMonth in range(1,inMonth):
        allDay += monthDay(inYear,allMonth)
    allDay+=1
    return allDay
#输入年份月份到1900年-1-1的总天数
def ymallDay(inYear,inMonth):
    return allYearDay(inYear)+allMonthDay(inYear,inMonth)

#计算输入年月的第一天的星期数,输出这个月的天数
def weekNum(inYear,inMonth):
    #list = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
    weekN = ymallDay(inYear,inMonth)%7
    monthD = monthDay(inYear,inMonth)
    return weekN,monthD

#输出日历
    # 先输出提示语句,接受用户输入的年、月
inYear = int(input("请输入年份:"))
inMonth = int(input(("请输入月份:")))
weekN,monthD = weekNum(inYear,inMonth)
print("星期日  星期一  星期二  星期三  星期四  星期五  星期六")
for i in range(weekN):
    print("\t", end="\t")
#打印日期
for d in range(1, monthD+1):
    print(d, end="\t\t")
    #考虑换行
    if(weekN + d) % 7 == 0:
        print()
input()

你可能感兴趣的:(万年历Python的入门编写)