r()判断闰年
def r(n):
if n%4 == 0 and n%100 != 0 or n%400 == 0:
return True
else:
return False
t()计算是第几天
def t(y,m,d):
m1 = [31,28,31,30,31,30,31,31,31,30,31]
m2 = [31,29,31,30,31,30,31,31,31,30,31]
if r(y):
days = sum(m2[:m-1]) + d
else:
days = sum(m1[:m-1]) + d
return days
per()预获取处理输入的年份
def per():
n = input('输入年月日,如20180402:') or '20180402'
y = int(n[:4])
m = int(n[4:6])
d = int(n[6:])
return y, m, d
获取日期输出天数
while True:
y, m, d = per()
days = t(y, m, d)
print('%d年%d月%d日,是%d年的第%d天\n\n'%(y,m,d,y,days))