""" 用户可以输入"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位):")continueelse: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 = Trueif month < 1 or month > 12: # 判断月份是否合法print("你输入的%s不是有效日期,请重新输入:" % month)continueif isRunNian:# 判断日期是否合法if day < 1 or day > pingnian_month[month]:print("你输入的%s不是有效日期,请重新输入:" % date)continueelse:# 判断日期是否合法if day < 1 or day > runnian_month[month]:print("你输入的%s不是有效日期,请重新输入:" % date)continueprint("你输入的%s是有效日期" % date)print("=" * 30)
上一篇:了解webpack