要使用list
请输入年份:
请输入总天数:
年第天是月日
希望有人能给我详细的解释,万分感谢!
说白了,就是个闰年平年判断的问题
代码给你注释了
import datetime
year = int(input("请输入年份:"))
total_days = int(input("请输入总天数:"))
# 创建一个包含每个月份对应天数的列表
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 判断是否为闰年,如果是,则将二月的天数修改为29
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
month_days[1] = 29
# 初始化月份和日期
month = 1
day = total_days
# 遍历每个月份,减去对应的天数,直到剩余天数小于等于当前月份的天数
for i, days in enumerate(month_days):
if day <= days:
month = i + 1
break
else:
day -= days
# 输出结果
output = f"{year}年第{total_days}天是{month}月{day}日"
print(output)
不知道你这个问题是否已经解决, 如果还没有解决的话: