def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
def days_in_month(year, month):
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month in [4, 6, 9, 11]:
return 30
elif is_leap_year(year):
return 29
else:
return 28
def day_of_year(year, month, day):
total_days = 0
for m in range(1, month):
total_days += days_in_month(year, m)
total_days += day
return total_days
date_input = input("请输入日期(格式:xxxx 年 xx 月 xx 日):")
year, month, day = map(int, date_input.replace("年", " ").replace("月", " ").replace("日", "").split())
day_number = day_of_year(year, month, day)
print(f"日期 {year}年{month}月{day}日 是当年的第 {day_number} 天")
Python 语言编写的程序,用于计算输入日期是当年的第几天
最新推荐文章于 2024-04-02 19:07:17 发布