#-*- coding:utf- -*-
'''
Created on --
# 输入年月日,判断为一年的第几天
@author: AdministrInputator
''' def leapYear(year): # 判断平闰年,由于输入年份只有两位数,‘’~‘’转换为2000~,‘’~’‘转换为1970~
if year % == and year % != :
return True
else:
return False def dateJudge(strInput): # which day of the year
year = int(strInput[:])
month = int(strInput[:])
day = int(strInput[-:])
daysLeapYear = [, , , , , , , , , , , ] # days of months in leap year
daysNonLeapYear = [, , , , , , , , , , , ]
theDay = # the result
if len(strInput) != :
print(' 6 numbers limited!','\n','=-'*)
return
if month > :
print(' the month is not more than 12!','\n','=-'*)
return
if leapYear(year):
if day > daysLeapYear[month - ]:
print(' days of the month ',month,' cant\'t be larger than ',daysLeapYear[month - ],'!\n','=-'*)
return
if not leapYear(year):
if day > daysNonLeapYear[month - ]:
print(' days of the month ',month,' cant\'t be larger than ',daysNonLeapYear[month - ],'!\n','=-'*)
return
# calculate the day
if year < :
year +=
else:
year +=
if leapYear(year):
for index in range(, month - ):
theDay += daysLeapYear[index]
else:
for index in range(, month - ):
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','=-'*)
# function call
dateJudge('')
dateJudge('')
dateJudge('')
dateJudge('')
dateJudge('')
dateJudge('')
运行结果:
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!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>>>