python输入某年某月某日,判断这一天是这一年的第几天?

- 普通年份若能被4整除则为闰年;- 若年份能被100整除,则需能被400整除才是闰年。输入年份与月份 year = int(input("请输入年份:"))month = int(input("请输入月份:"))day = int(input("请输入日期:"))判断闰年 def is_leap_year(year):if year % 4 == 0 and (year % 100 !=...
python输入某年某月某日,判断这一天是这一年的第几天?
# 判断闰年公式
- 普通年份若能被4整除则为闰年;
- 若年份能被100整除,则需能被400整除才是闰年。
## 输入年份与月份
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))
## 判断闰年
def is_leap_year(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return False
## 初始化月份天数
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
leap_year_days_in_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
## 计算截至当前月份的天数
if is_leap_year(year):
current_days = sum(leap_year_days_in_month[:month - 1])
else:
current_days = sum(days_in_month[:month - 1])
## 加上当前日期,得出总天数
total_days = current_days + day
## 输出结果
print(f"今天是今年的第 {total_days} 天。")2025-01-09
mengvlog 阅读 6 次 更新于 2025-07-19 02:27:04 我来答关注问题0
檬味博客在线解答立即免费咨询

Python相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部