#python作业练习
#老马指导
import datetime
import time
tod = datetime.datetime.now()
toyear = int(tod.strftime("%Y"))
tomon = int(tod.strftime("%m"))
todayy = int(tod.strftime("%d"))
today = tod.strftime("%Y-%m-%d")
print("今天是:"+today)
Mon = [0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def Modify_Mon(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
Mon[2] = 29
else:
Mon[2] = 28
def ins_year():
input_year = int(input())
if input_year <= toyear:
Modify_Mon(input_year)
return input_year
else:
print("请输入正确年份")
return ins_year()
def ins_mon():
input_mon = int(input())
if year == toyear:
limit_mon = tomon
else:
limit_mon = 12
if input_mon >= 1 and input_mon <= limit_mon:
return input_mon
else:
print("请输入正确月份")
return input_mon()
def ins_day():
input_day = int(input())
if year == toyear and mon == tomon:
limit_day = today
else:
limit_day = Mon[mon]
if input_day >= 1 and input_day <= limit_day:
return input_day
else:
print("请输入正确日期")
return ins_day()
def how_rlong(toyear,mon,day):
if toyear % 4 == 0 and toyear % 100 != 0 or toyear % 400 == 0:
num = 0
return num
else:
next_year = int(toyear)+1
while(next_year % 4 != 0 or next_year % 100 == 0 and next_year % 400 != 0):
next_year = next_year+1
str1 = str(next_year) + "-" + str(mon) + "-" + str(day) #下一个闰年生日日期
date1 = datetime.datetime.strptime(today[0:10],"%Y-%m-%d") #今日时间元组
date2 = datetime.datetime.strptime(str1[0:10],"%Y-%m-%d") #下一个闰年生日时间元组
num = (date2-date1).days
return num
def how_long(toyear,mon,day):
next_year = int(toyear) + 1 #明年年份
str1 = str(next_year) + "-" + str(mon) + "-" + str(day) #明年生日日期
str2 = str(int(toyear)) + "-" + str(mon) + "-" + str(day) #今年生日日期
date1 = datetime.datetime.strptime(today[0:10],"%Y-%m-%d") #今日时间元组
date2 = datetime.datetime.strptime(str1[0:10],"%Y-%m-%d") #明年生日时间元组
date3 = datetime.datetime.strptime(str2[0:10],"%Y-%m-%d") #今年生日时间元组
if mon == tomon:
if day == todayy:
num = 0
if day > todayy:
num = (date3-date1).days
if day < todayy:
num = (date2-date1).days
elif mon < tomon:
num = (date2-date1).days
else:
num = (date3-date1).days
return num
if __name__ == "__main__":
print("输入出生年份:", end = "")
year = ins_year()
print("输入月份:", end = "")
mon = ins_mon()
print("输入日期:", end = "")
day = ins_day()
myb = str(year) + "-" + str(mon) + "-" + str(day)
print("出生日期为:" + myb)
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 and mon==2 and day==29:
num = how_rlong(toyear,mon,day)
else:
num = how_long(toyear,mon,day)
if num != 0:
print("距离生日还有" + str(num) + "天")
else:
print("今日生日")
今天是:2023-09-21
输入出生年份:2020
输入月份:2
输入日期:29
出生日期为:2020-2-29
距离生日还有161天