注释写的挺详细的,不懂的问我就好。
# 判断是否是闰年
def leap_year(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
def which_day(year, month, day):
days_of_month = [
# 平年
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
# 闰年
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
][leap_year(year)] # 返回值要么为0要么为1
total = 0
# 月份需要减1,遍历0到输入的月份,累加天数
for index in range(month - 1):
total += days_of_month[index]
return total + day
# 输出输入日期是今年的第几天
print(which_day(2020, 11, 28))