Python implementation of simple perpetual calendar example sharing _python

Source: Internet
Author: User

Copy Code code as follows:
#!/usr/bin/env Python2
#-*-Coding:utf-8-*-
__author__ = ' Jalright '

"""
Implementing a calendar with Python
"""

Def is_leap_year (year):
"""
Judge whether it is a leap year, return a Boolean value
"""
If Year/4==0 and year/400!=0:
Return True
elif year/100 = = 0 and year/400 ==0:
Return True
Else
Return False

def getmonthdays (Year,month):
"""
How many days are there in the month of the specified date
"""
Days = #31天居多, set to default value
If month = 2: #2月份要判断是否是闰年
If Is_leap_year (year):
days=29
Else
days=28;
Elif month in [4,6,9,11]: #判断小月, only 30 days
Days=30
Return days

def gettotaldays (Year,month):
"""
Get 1990-01-01 from now how many days, 1990-01-01 is Monday, this is the standard to judge the week
"""
Totaldays=0
For I in Range (1990,year): #使用range来循环, figure out how many years and how many days
If Is_leap_year (i): #判断是否是闰年
Totaldays + 366
Else
Totaldays + 365
For I in Range (1,month): #使用range循环, figure out how many days before the first few months of this year
Totaldays +=getmonthdays (year,i)
Return totaldays


if __name__ = = ' __main__ ':
While True: #循环判断是否输入错误的格式
Print "Xxxxxxxxxxpython realize million Almanac xxxxxxxx"
Year = raw_input ("Please enter years (such as: 1990):")
month = raw_input ("Please enter month: such as: 1")
Try: #捕捉输入异常格式和月份的正确
year = Int (year)
month = Int (month)
If month <1 or month >1: #判断月份是否输入错误, the error starts the loop again
Print "Year or month input error, please re-enter!" "
Continue
Except: #捕捉到转换成整型异常, output prompt, restart cycle
Print "Year or month input error, please re-enter!" "
Continue
Break #如果没有异常就跳出循环
#if is_leap_year (year):
# print '%s is run year '%year
#else:
# print '%s is excepting '%year
#print "%s Month total%s days!" "% (Month,getmonthdays (year,month))

print "day \ t two \ t three t four \ t five \ t six"
icount = 0 #计数器来判断是否换行
For I in range (Gettotaldays (year,month)%7):
print ' \ t ', #输出空不换行
Icount+=1
For I in range (1,getmonthdays (year,month)):
Print I,
print ' \ t ',
Icount +=1
If icount%7 = 0: #计数器取余为0, linefeed
print '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.