Python | 万年历

一、功能描述:

  • 查询日期如:wnl.py 2007/01/01
  • 显示月日历:wnl.py 2007/01

二、代码:

from sys import *


Weeks = ['Sun', "Mon", 'Tus', 'Wed', 'Thu', 'Fri', 'Sat']   
    
try :
    date_in = argv[1]
except:
    print('\n ERROR! you forgot to enter the date you want to view\n')
    date_in = input("Please input a date!\n > ")
    
# print(date_in)
year, month, day = date_in.split('/')
try :
    year, month, day = int(year), int(month), int(day)
except :
    year = int(year)
    month = int(month)
    day = 0


# print(year, month, day)
if month < 1 or month > 12:
    print('\n ERROR! the entered MONTH is invalid\n')
    exit()


if year > 2000:
    d = (year-1-2000)//4-(year-1-2000)//100+(year-1-2000)//400+1
elif year == 2000:
    dy = 0
    m2 = 1
else:
    d = (year-2000)//4-(year-2000)//100+(year-2000)//400


dy=(year-2000)+d
if (year%4==0 and year%100 != 0) or (year%100==0 and year%400==0):
    m2 = 1
else:
    m2 = 0


if month == 1 :
    dm = 0
    month = 31
elif month == 2 :
    dm = 3
    if d == 1 :
        month = 29
    else:
        month = 28
elif month == 3 :
    dm = 3 + m2
    month = 31
elif month == 4 :
    dm = 6 + m2
    month = 30
elif month == 5 :
    dm = 1 + m2
    month = 31
elif month == 6 :
    dm = 4 + m2
    month = 30
elif month == 7 :
    dm = 6 + m2
    month = 31
elif month == 8 :
    dm = 2 + m2
    month = 31
elif month == 9 :
    dm = 5 + m2
    month = 30
elif month == 10 :
    dm = m2
    month = 31
elif month == 11 :
    dm = 3 + m2
    month = 30
elif month == 12 :
    dm = 5 + m2
    month = 31
else:
    print("What is it?")


if day < 0 or day > month :
    print("\n Error! The entered Day is invalid\n")
    exit()


week = (dy + dm + day - 1 + 6) % 7


if week < 0 :
    week += 7


if day > 0:
    if week == 0:
        WEEK = Weeks[0]
    elif week == 1:
        WEEK = Weeks[1]
    elif week == 2:
        WEEK = Weeks[2]
    elif week == 3:
        WEEK = Weeks[3]
    elif week == 4:
        WEEK = Weeks[4]
    elif week ==5:
        WEEK = Weeks[5]
    elif week == 6:
        WEEK = Weeks[6]
    else:
        pass
    print("\n this day is ", WEEK, "(",int(week),")"+ "\n OK!\n")
else:
    week = (week+1) % 7
    print("\n the calender of this month as following")
    print("\n ***************************************")
    print(" SUN  MON  TUR  WEN  THU  FRI  STA\n")
    for i in range(int(week)):
        print("    ", end = ' ')
    for i in range(1, month+1):
        print(" %2d "%i, end = ' ')
        if (week+1) % 7 == 0 and i != month :
            print("\n")
        week = week + 1
    print("\n****************************************\n")
    print("\n OK! \n")

三、代码拆解:

参数获取:

  • 如果想要从操作系统获取参数,需要使用sys模块。
  • 为使调试方便,增加了手动输入参数的功能,输入参数的格式应该正确,否则会报错
    如:2007/01/01
    或:2007/01/

日期判断:

  • 判断日期,使用了dy,dm等参数表示一年中的日差和一月中的日差。
  • 使用到的公式:
    week = (dy + dm + day - 1 + 6) % 7
    dy=(year-2000)+d
    
    
    if year > 2000:
        d = (year-1-2000)//4-(year-1-2000)//100+(year-1-2000)//400+1
    elif year == 2000:
        dy = 0
        m2 = 1
    else:
        d = (year-2000)//4-(year-2000)//100+(year-2000)//400

代码备注:

  • print()打印月日历时,将end设置为空格。否则会跳跃,每行仅剩一个数字
  • dm从每个月的判断中获得,由于python未提供switch相关的功能,这里只能使用if-elif-else格式
  • 为精简代码行数,可以使用“;”使一行中可以写下多个语句
  • 该程序中基本年为2000年。

你可能感兴趣的:(Python)