python 判断是否是有效的日期
python 判断是否是有效的日期
·
""" 用户可以输入"20170327"等三种格式的日期 判断是否是有效日期,如"20170229"不是有效日期,"20171345"不是有效日期 """ pingnian_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] runnian_month = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] while True: date = input("请输入一个日期(8位):") # 如果输入QUIT则退出 if date == "QUIT": break # 如果长度不为8或者不是纯数字,则重新输入 if len(date) != 8 and not date.isdigit(): print("请输入一个日期(8位):") continue else: year = int(date[0:4]) # 截取年份 month = int(date[4:6]) # 截取月份 day = int(date[6:]) # 截取日期 isRunNian = False # 判断是否是闰年 if year % 4 == 0 and year % 100 != 0 and year % 400 == 0: isRunNian = True if month < 1 or month > 12: # 判断月份是否合法 print("你输入的%s不是有效日期,请重新输入:" % month) continue if isRunNian: # 判断日期是否合法 if day < 1 or day > pingnian_month[month]: print("你输入的%s不是有效日期,请重新输入:" % date) continue else: # 判断日期是否合法 if day < 1 or day > runnian_month[month]: print("你输入的%s不是有效日期,请重新输入:" % date) continue print("你输入的%s是有效日期" % date) print("=" * 30)

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)