# 计算某个日期是一年的多少天 days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] date = input('请输入日期,格式如2024-07-21:').split('-') year = int(date[0]) month = int(date[1]) day = int(date[2]) # 判断是否是闰年,如果是,则2月加1天 if (not year % 4 and year %100 ) or not year % 400: days[2] = 29 print('闰年') result = 0 # 开始计算天数,利用循环遍历天数后相加 # for i in range(month): # result += days[i] # result += day # 直接求和 result = sum(days[0:month]) + day print('%d年%d月%d日是%d年的第%d天' % (year, month,day, year, result))
最后编辑:2024年07月21日
©著作权归作者所有
最新回复