Python输入出生年月日计算下个生日距离今天还有多少天

编程入门 行业动态 更新时间:2024-10-27 20:37:38

Python输入出生年月日计算<a href=https://www.elefans.com/category/jswz/34/1756917.html style=下个生日距离今天还有多少天"/>

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输入出生年月日计算下个生日距离今天还有多少天

本文发布于:2024-02-11 03:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1678994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下个   年月日   距离   生日   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!