# 设计一个函数传入年月日返回这一天是这一年的第几天
def is_leap_year(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
# 2018 1 1 --> 1
# 2016 3 1 -->61
# 1980 11 28 ---> 333 #闰年
# 1981 12 31 --->365
def which_day(year, month, day):
list1 = [31,29,31,30,31,30,31,31,30,31,30,31]#闰年
list2 = [31,28,31,30,31,30,31,31,30,31,30,31]#平年
#高端玩法
#day_of_month = [[31,29,31,30,31,30,31,31,30,31,30,31],[31,28,31,30,31,30,31,31,30,31,30,31]][is_leap_year(year)]
#total = 0
#for index in range(month - 1):
# total += day_of_month[index]
#return total + date
total = 0
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
for index in range(month - 1):
total += list1[index]
else:
for index in range(month - 1):
total += list2[index]
return total + day
if __name__ == '__main__':
print(which_day(1980,12,31))
设计一个函数传入年月日返回这一天是这一年的第几天
最新推荐文章于 2024-07-30 23:37:19 发布