其他类似题有一个bug是输入的天数刚好是在星期天的时候不会显示7,而是0.此处已经修改。
#判断是否是闰年
def Is_leap_year(year):
if year%400==0 or year%4==0 and year%100!=0:
return True
else:
return False
#标记每个月份的时间
def Days_in_months(year,month):
if month in (1,3,5,7,8,10,12):
return 31
elif month in (4,6,9,11):
return 30
elif Is_leap_year(year):
return 29
else:
return 28
def Total_days(year,month,day):
days=0
for y in range(1800,year):
if Is_leap_year(y):
days+=366
days%=7
else:
days+=365
days%=7
for m in range(1,month):
days+=Days_in_months(year,m)
days%=7
purpose=(days+day+2)%7
if purpose==0:
return 7
return (days+day+2)%7
year=int(input('请输入年份:'))
month=int(input('请输入月份:'))
day=int(input('请输入号数:'))
print("此日星期:",Total_days(year,month,day))
结果如下: