Python implementation of the simple calendar example sharing

  • 2020-04-02 13:36:07
  • OfStack

#!/usr/bin/env python2
#-*- coding:utf-8 -*-
__author__ = 'jalright'
"""
 use python Implement the perpetual calendar 
"""
def is_leap_year(year):
    """
 To determine if it is a leap year, return 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):
    """
 Gets the number of days in a given month 
    """
    days = 31        #31 Most days, set to default 
    if month == 2 :    #2 The month determines whether it is a leap year 
        if is_leap_year(year):
            days=29
        else:
            days=28;
    elif month in [4,6,9,11]:     # Judge the month, only 30 day 
        days=30
    return days
def getTotalDays(year,month):
    """
 To obtain 1990-01-01 How many days till now, 1990-01-01 It's Monday, and that's the standard for the week 
    """
    totalDays=0
    for i in range(1990,year):     # use range So let's do the cycle. Let's figure out how many years and how many days 
        if is_leap_year(i):        # Decide if it's a leap year 
            totalDays += 366
        else:
            totalDays += 365
    for i in range(1,month):       # use range Cycle, and figure out how many days have passed in the first few months of the year 
        totalDays +=getMonthDays(year,i)
    return totalDays

if __name__ == '__main__':
    while True:                                 # The loop determines if the format is entered incorrectly 
        print " X x x x x x x x x x python Achieve calendar ×××××××× "
        year = raw_input(" Please enter the year (e.g. : 1990 ) : ")
        month = raw_input(" Please enter month: such as: 1")
        try:                                    # Capture the correct input exception format and month 
            year = int(year)
            month = int(month)
            if month <1 or month >1:            # Determine if the month is entered incorrectly, and the error starts the loop again 
                print " Year or month input error, please re-enter! "
                continue
        except:                                 # Catches an exception converted to an integer, prints a prompt, and starts the loop again 
            print " Year or month input error, please re-enter! "    
            continue
        break     # If there are no exceptions, break out of the loop 
    #if is_leap_year(year):
    #    print "%s Is a leap year "%year
    #else:
    #    print "%s Is a leap year "%year
    #print "%s Month total %s Day! "%(month,getMonthDays(year,month))

    print " day t one t two t three t four t five t six "
    iCount = 0      # A counter to determine if there is a line break 
    for i in range(getTotalDays(year,month)%7):
        print 't',                 # The output is blank and does not wrap 
        iCount+=1
    for i in range(1,getMonthDays(year,month)):
        print i,
        print 't',
        iCount +=1
        if iCount%7 == 0 :           # The counter takes the remainder of 0, A newline 
            print ''


Related articles: