思路
- 先判断是闰年还是平年,再调用相应函数
- 计算每个月多少天,然后加上前几个月的所有天数和这个月的天数
import sys
#输入某年某月某日,计算这一天是这一年的第几天
while True:
year=int(input('Year:'))
mon=int(input('Month:'))
day=int(input('Day:'))
if year<0 or mon>12 or mon<0 or day>31 or day<0:
#判断输入数值是否超过正常范围你,若是,报错退出程序
print("Day Error!")
sys.exit(0)
if year%4==0 and year%400!=0:
#判断是平年还是闰年
run_year=True
else:
run_year=False
def day_run_year(year,mon,day):
"""定义闰年的月份,并返回总天数"""
sum_day=day
day_month=[0,31,29,31,30,31,30,31,31,30,31,30,31]
for i in day_month[0:mon]:
sum_day=sum_day+i
return sum_day
def day_ping_year(year,mon,day):
"""定义平年每月是多少天,并返回总天数"""
sum_day=day
day_month=[0,31,28,31,30,31,30,31,31,30,31,30,31]
for i in day_month[0:mon]:
sum_day=sum_day+i
return sum_day
#调用函数,输出总天数
if run_year:
print(day_run_year(year,mon,day))
else:
print(day_ping_year(year,mon,day))