异步,永不止步

Practice:输入年月日,判断该时间为一年的第几天

#-*- coding:utf-8 -*-
'''
Created on 2015-6-7
# 输入年月日,判断为一年的第几天
@author: AdministrInputator
'''

def leapYear(year): # 判断平闰年,由于输入年份只有两位数,‘00’~‘69’转换为2000~2069,‘70’~’99‘转换为1970~1999
    if year % 4 == 0 and year % 400 != 0:
        return True
    else:
        return False

def dateJudge(strInput): # which day of the year
    year = int(strInput[:2])
    month = int(strInput[2:4])
    day = int(strInput[-2:])
    daysLeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # days of months in leap year
    daysNonLeapYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    theDay = 0 # the result
    if len(strInput) != 6:  
        print('    6 numbers limited!','\n','=-'*20)
        return
    if month > 12:
        print('    the month is not more than 12!','\n','=-'*20)
        return
    if leapYear(year):
        if day > daysLeapYear[month - 1]:
            print('    days of the month ',month,' cant\'t be larger than ',daysLeapYear[month - 1],'!\n','=-'*40)
            return
    if not leapYear(year):
        if day > daysNonLeapYear[month - 1]:
            print('    days of the month ',month,' cant\'t be larger than ',daysNonLeapYear[month - 1],'!\n','=-'*40)
            return
    # calculate the day
    if year < 70: 
        year += 2000
    else:
        year += 1900
    if leapYear(year):
        for index in range(0, month - 1):
            theDay += daysLeapYear[index]
    else:
        for index in range(0, month - 1):
            theDay += daysNonLeapYear[index]
    theDay += day # add the day inputted to the result
#     print(type(year))
    print('    Input time:',year,'/',month,'/',day, ';''\n    result:',theDay,'\n','=-'*20)
# function call
dateJudge('150228')
dateJudge('710509')
dateJudge('151231')
dateJudge('1502232')
dateJudge('150245')
dateJudge('152228')
    

运行结果:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Input time: 2015 / 2 / 28 ;
result: 59
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Input time: 1971 / 5 / 9 ;
result: 129
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Input time: 2015 / 12 / 31 ;
result: 365
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
6 numbers limited!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
days of the month 2 cant't be larger than 28 !
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
the month is not more than 12!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>>>

posted on 2015-06-07 14:08  gaopq  阅读(622)  评论(0编辑  收藏  举报

导航