python百炼成钢实例003-这天是第几天-注意个闰月一个日期输出格式就好

实例004:这天第几天
题目 输入某年某月某日,判断这一天是这一年的第几天? 闰年每隔4年一次。 平年的2月是28天,闰年2月是29天。 4月、6月、9月、11月各是30天。 1月、3月、5月、7月、8月、10月、12月各是31天。
"""
实例004:这天第几天
题目 输入某年某月某日,判断这一天是这一年的第几天?

闰年每隔4年一次。 平年的2月是28天,闰年2月是29天。 4月、6月、9月、11月各是30天。 1月、3月、5月、7月、8月、10月、12月各是31天。
"""

def is_leap_year(year):
    return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0)


monDay = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))

index_day = 0
for i in range(month):
    index_day += monDay[i]
if month > 2 and is_leap_year(year): # 如果月份大于2,则判断是否是闰年,闰年2月多加一天
    index_day += 1
index_day = index_day + day
print("{}-{:0>2}-{:0>2}是这一年的第{}天".format(year, month, day, index_day)) # 注意月份为个位时,左侧补零的格式

结果查看:

D:Esoftpython.exe D:/E/soft/pythoncode/pythonWork/Case1_10/04index_day.py
请输入年份:1990
请输入月份:5
请输入日期:2
1990-05-02是这一年的第122天
原文地址:https://www.cnblogs.com/shishibuwan/p/15514513.html