輸入年月日計算這是這一年的第幾天.
作者:由 鐵匠 發表于 曲藝時間:2022-11-19
“”“
要求:輸入年月日計算這是這一年的第幾天。
演算法:前幾個月的總天數 + 當月天數
”“”
year = int(input(“請輸入年:”))
month = int(input(“請輸入月:”))
day = int(input(“請輸入日:”))
day_of_second = 29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28
days_of_month = (31, day_of_second, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# 累加前幾個月的總天數
# total_days = 0
# for i in range( month -1 ):# 0 1 2 3
# total_days += days_of_month[i]
total_days = sum(days_of_month[: month - 1])
# 累加當月天數
total_days += day
print(“這是一年的第”+str(total_days)+“天”)