一个软件的关键在于

你想用它来做什么

一、功能描述查询日期如: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
if month < 1 or month > 12:
print('\n ERROR! the entered MONTH is invalid\n')
exit()
# 判断闰年,同时得到d用来计算日差,m2为闰月标记
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等参数表示一年中的日差和一月中的日差。m2为闰月的标志以及作为一个日差来使用。

使用到的公式: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年。

四、笔者评述

这是一个简单小程序,从这个程序说明,对于编程而言,有一定的数学基础是比较重要的,除此之外锻炼逻辑思维能力可以提高编程能力。

从实用性角度来讲,这并不是一个有用的程序,但是作为学习,是非常不错的。

代码的优化力度不大,但是可优化空间很大,这是一个简单的实现,从操作性上来讲,比较头疼,比如,不能连续查询,对参数格式比较依赖等。

五、思考

关于此代码的不足,请读者们在学习或者作为积累代码库的时候,尝试修改代码,比如给代码添加连续查询功能,让参数的格式变得自由,或者给程序带上参数,增加新的功能等。