输入( 年 ,月 )打印日历()

def is_leapyear( year ):
    if year % 4 ==  0 and year % 100 !=  0 or year % 400 == 0 :
        return True
    return False

def days_year( year ):
    if is_leapyear( year ):
        return 366
    else:
        return 365

def days_month( year , month ):
    l = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
    u = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
    if is_leapyear( year ):
        return l[ month-1 ]
    else :
        return u[ month-1 ]

def get_week( year , month ):
    days = 0
    if ( 1990 <= year <= 2018 ) or ( 1 <= month <= 12 ) :
        for y in range( 1990 , year ):
            days += days_year( y )
        for m in range( 1 , month ):
            days += days_month( year , m )
        return ( days + 1 ) % 7

while True:
    year , month = eval( input( ' Input year and month (year , month): ' ) )
    days = 0
    if ( 1990 <= year <= 2018 ) or ( 1 <= month <= 12 ):
        print( '\t{}年{}月'.format(year,month) )
        print( '\t日\t一\t二\t三\t四\t五\t六' )
        for week in range( get_week( year , month ) ):
            print( '\t'+' ',end='' )
        for day in range( 1 , days_month( year , month ) + 1 ):
            print( '\t{: <2}'.format( day ) , end='' )
            if ( day + get_week( year , month ) ) % 7 == 0 or day == days_month( year , month ) :
                print('')
    else :
        continue

猜你喜欢

转载自www.cnblogs.com/yangking/p/10075678.html