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

# -*- coding: utf-8 -*-

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

import time,datetime

def input_date():

    leapYear = False

    while True:
        year = raw_input('input year(yyyy):')
        if len(year) == 4 and year.isdigit() and year > 0:
            if (int(year) % 400 == 0) or (int(year) % 100 != 0 and int(year) % 4 == 0):
                leapYear = True
                print 'It is leap year'
            else:
                print 'It is not leap year'
            print "your input year is:",year
            break
        else:
            print 'your input is error,input year(yyyy) please!'
    #4位,数字,大于o

    while True:
        month = input('input month(mm):')
        if 0<month<13:
            print "your input month is:",month
            break
        else:
            print 'your input is error,input year(0<mm<13)again please!'

    while True:
        date = input('input date(dd):')
        if month in [1,3,5,7,8,10,12]:
            if 1 <= date <= 31:
                bigMonth = True
                #print ('your input dates is:%s,%s,%s' % (year, month, date))
                break
            else:
                print 'your input is error,input date(1<=dd<=31) again please!'
        elif month in [4,6,9,11]:
            if 1<= date <= 30:
                #print ('your input dates is:%s,%s,%s' % (year, month, date))
                break
            else:
                print 'your input is error,input date(1<=dd<=30) again please!'
        elif month == 2:
            if leapYear:
                if 1 <= date <= 29:
                    #print ('your input dates is:%s,%s,%s' % (year, month, date))
                    break
                else:
                    print 'your input is error,input date(1<=dd<=29) again please!'
            else:
                if 1 <= date <= 28:
                    #print ('your input dates is:%s,%s,%s' % (year,month,date))
                    break
                else:
                    print 'your input is error,input date(1<=dd<=28) again please!'

    print ('your input dates is:%s/%s/%s' % (year, month, date))

    if month == 1:
        numDay = date
    elif month == 2:
        numDay = 31 + date
    elif month == 3:
        if leapYear:
            numDay = 31 + 29 + date
        else:
            numDay = 31 + 28 + date
    elif month == 4:
        if leapYear:
            numDay = 31 + 29 + 31 + date
        else:
            numDay = 31 + 28 + 31 + date
    elif month == 5:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + date
    elif month == 6:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + date
    elif month == 7:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + date
    elif month == 8:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date
    elif month == 9:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date
    elif month == 10:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date
    elif month == 11:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date
    elif month == 12:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date

    print 'It is No.%s day' % numDay
#def calculate_date():

input_date()

主要考虑几点:
1、无论是年、月、日在输入过程中都判断是否合法,否则重新输入;
3、判断闰年;
2、在计算第几天的时候目前的实现应该还是很挫….

发表评论

电子邮件地址不会被公开。 必填项已用*标注