# -*- coding:utf-8 -*-
'''
题目:输入某年某月某日,判断这一天是这一年的第几天?
'''
month = [m for m in range (1, 13)]
day = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
yearGet = int (raw_input ("Enter the time:year:"))
monthGet = int (raw_input ("Enter the time:month:"))
dayGet = int (raw_input ("Enter the time:day:"))
if monthGet not in month:
raise ValueError ("Month should between 1 and 12.")
if monthGet == 2: #判断2月份,天数不得超过29天
if dayGet > 29:
raise ValueError ("Current month can hold just 28 days at most,29 days for runYeay.")
if monthGet in (4, 6, 9, 11): #判断4, 6, 9, 11月,天数不得超过30天
if dayGet > 30:
raise ValueError ("Current month can hold just 30 days at most.")
if dayGet not in range (1, 32):
raise ValueError ("Out of bound of day.")
sumDay = 0
sumDay = sum (day [:monthGet]) + dayGet # 计算当前月份的天数以及当前月份之前所有月份天数的合计
# 判断闰年,若是闰年,+1天
#闰年:四年一闰;百年不闰,四百年再闰
# 整百的年份,只有能被400整除时才是闰年;非整百的年份,能被4整除的为闰年
#(将判断闰年写成一个函数)
def runYear(getYear):
if yearGet % 400 == 0:
return True
elif yearGet % 100 != 0:
if yearGet % 4 == 0:
return True
else:
return False
leap = 0
if runYear(yearGet):
leap = 1
'''
if yearGet % 400 == 0:
leap = 1
elif yearGet % 100 != 0:
if yearGet % 4 == 0:
leap = 1
'''
if leap == 1:
sumDay += 1 #若年份是闰年,则总天数+1天
if yearGet < 0:
yearGet = "公元前 "+str(abs(yearGet))
print "\n",30*"*"
print "Date:\n\t%s 年 %2d 月 %2d 日"%(yearGet,monthGet,dayGet)
print "It's ", sumDay, "th day of the year %s."%yearGet
输入某年某月某日,判断这一天是这一年的第几天
最新推荐文章于 2023-10-08 20:19:06 发布