# 闰年规则:四年一闰,百年不闰,四百年再闰 # 定义函数,判断是否为闰年 def leapyear(year): return (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) # 定义一个列表,储存每月天数 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 储存月份的天数 total = 0 while True: # 由用户输入年、月、日 year = int(input('请输入四位数年份:')) month = int(input('请输入月份:')) day = int(input('请输入日期:')) # 限制月份、日期范围 if 0 < month <= 12 and 0 < day <= 31: print('您输入的月份和日期正确') if leapyear(year): # 若是闰年,二月改为29天 days[1] = 29 # 遍历列表,对应月份的天数传给tatal存储 for i in range(month - 1): total += days[i] break else: print('您输入的月份或日期不正确,请重新输入') print('这是{}年的第{}天'.format(year, total + day))