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

# -*- 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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值