zoukankan      html  css  js  c++  java
  • 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!
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    >>>

  • 相关阅读:
    什么是php面向对象及面向对象的三大特性
    php类的定义与实例化方法
    php面向对象之$this->用法简述
    url的主要功能是什么
    PHP字符串比较函数详解
    PHP截取字符串函数substr()函数实例用法详解
    php 读取文件
    php 正则达达示中的模式修正符
    php正则表示中的元字符
    php 正则表达示中的原子
  • 原文地址:https://www.cnblogs.com/peiqianggao/p/4558403.html
Copyright © 2011-2022 走看看