在控制檯中輸入月,日. 計算這是一年的第幾天.(Python)

#在控制檯中輸入月,日.
#        計算這是一年的第幾天.
# 例如:3月5日
#      累加1月,2月總天數,再累加3月的5天.
# 例如:5月10日
#      累加1月,2月,3月,4月總天數,再累加5月的10天.
# month = int(input("請輸入月份:"))
# day = int(input("請輸入天:"))
# day_of_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# result = 0
# # 累加前幾個月
# for i in range(month - 1):
#     result += day_of_month[i]
# # 累加當月
# result += day
# print(result)


month = int(input("請輸入月份:"))
day = int(input("請輸入天:"))
day_of_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# 累加前幾個月
result = sum(day_of_month[:month - 1])
# 累加當月
result += day
print(result)

輸出:

請輸入月份:5
請輸入天:8
128
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章