下个生日距离今天还有多少天"/>
Python输入出生年月日计算下个生日距离今天还有多少天
#python作业练习
#老马指导import datetime
import timetod = 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] = 29else:Mon[2] = 28def ins_year():input_year = int(input())if input_year <= toyear:Modify_Mon(input_year)return input_yearelse:print("请输入正确年份")return ins_year()def ins_mon():input_mon = int(input())if year == toyear:limit_mon = tomonelse:limit_mon = 12if input_mon >= 1 and input_mon <= limit_mon:return input_monelse:print("请输入正确月份")return input_mon()def ins_day():input_day = int(input())if year == toyear and mon == tomon:limit_day = todayelse:limit_day = Mon[mon]if input_day >= 1 and input_day <= limit_day:return input_dayelse:print("请输入正确日期")return ins_day()def how_rlong(toyear,mon,day):if toyear % 4 == 0 and toyear % 100 != 0 or toyear % 400 == 0:num = 0return numelse:next_year = int(toyear)+1while(next_year % 4 != 0 or next_year % 100 == 0 and next_year % 400 != 0):next_year = next_year+1str1 = 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).daysreturn numdef 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 = 0if day > todayy:num = (date3-date1).daysif day < todayy:num = (date2-date1).dayselif mon < tomon:num = (date2-date1).dayselse:num = (date3-date1).daysreturn numif __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天
更多推荐
Python输入出生年月日计算下个生日距离今天还有多少天
发布评论