原始问题:
使用Python编写代码:通过元组方式输入年月日,判断该日是年度中的第几天
# 获取用户输入的年份、月份和日期
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))
# 定义每个月的天数,考虑闰年情况
def get_month_days(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 计算到当前月之前的总天数
month_days = get_month_days(year)
day_count = sum(month_days[:month - 1]) + day
# 判断是否是闰年
is_leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
# 如果是闰年且月份大于2月,则天数加1
if is_leap_year and month > 2:
day_count += 1
# 输出结果
print(f"这一天是这一年的第{day_count}天")运行结果示例:
请输入年份:2021
请输入月份:3
请输入日期:15
这一天是这一年的第74天
Prev:金字塔原理第二篇第六章解读