Python Print Calendar

Source: Internet
Author: User

#未优化的代码
1 #Output Calendar2 defPrint_calendar (year,month,date = 1):3Month_dict = {'1':'January','2':'February','3':'March','4':'April','5':' May','6':'June','7':'July',4 '8':'August','9':'September','Ten':'October',' One':'November',' A':'December'}5 6 #converts a numeric month to a string and determines whether the month and number are legal7 ifMonthinchRange (1,13) andDateinchRange (1,31):8Month_str =Str (month)9 ifMonth_strinchmonth_dict:TenMonth_str =Month_dict[month_str] One Else: A Print('Invalid month or numbered input') - return-1 - the #Head - Print('%15s%8d'%(month_str,year)) - Print('-'*33) - Print('Sun Mon Tue Wed Thu Fri Sat') + - #get 1th Monthly number is the day of the week +First_day = Get_start_day (year,month,1) A #How many days are there to get this month atMonth_num =days_of_month (year,month) - -Each_day =0 - #Main body - forIndexinchRange (1,43): - in ifIndex < First_day + 1: - Print(' '*5,end ="') to Else: + if(index-1)% 7 = =0: - Print("') theEach_day + = 1 * ifEach_day >Month_num: $ returnFalsePanax Notoginseng ifEach_day < 10: - ifEach_day = =Date: the Print('%-5s'%('--'), end ="') + Else: A Print('%-4d'% (each_day), end ="') the Else: + ifEach_day = =Date: - Print('%-5s'%('--'), end ="') $ Else: $ Print('%-5d'% (each_day), end ="') - - the #Enter a month and day to determine the days of the week - #need a comparative standard: 2010-1-1 is FridayWuyi #calculate the current distance standard over how many days (total_days% 7 + 5-1)%7 the #The year is traversed first, leap years +366, not +365 - #then traverse the month, 31,30,29,28 Wu defGet_start_day (year,month,date): -Total_days =0 About #Traverse Year $ forOne_yearinchRange (2010, year): - ifis_leap_year (one_year): -Total_days + = 366 - Else: ATotal_days + = 365 + #print (total_days) the #Traverse Month - forOne_monthinchRange (1, month): $Total_days + =days_of_month (year,one_month) the #print (total_days) the #plus the number of the month, then how many days to find the total theTotal_days + =Date the - #What is the day of the month? inDay = (total_days% 7 + 5-1)% 7 the the #print (total_days) About #print (day) the return Day the the #Enter a year and month, and output how many days this month + #1,3,5,7,8,10,12--------31 days - #4,6,9,11--------------30 days the #if it is a leap year 2------------29 daysBayi #not leap Year 2-------------28 days the defDays_of_month (year,month): theDays =0 - ifMonthinch(1,3,5,7,8,10,12): -days = 31 the elifMonthinch(4,6,9,11): theDays = 30 the elifis_leap_year (year): thedays = 29 - Else: theDays = 28 the return Days the 94 defis_leap_year (year): the ifYear% 4 = = 0 andYear% 100! = 0orYear% 400 = =0: the returnTrue the returnFalse98 About defMain (): - Print('*'*33)101year = Int (input ('Please enter the year:'))102month = Int (input ('Please enter the month:'))103date = Int (input ('Please enter the numbers:'))104 Print('*'*33) the #How many days are there in a certain year?106 #Days = Days_of_month (year,month)107 #print (' {} year {} month has {} days '. Format (year,month,days))108 #the day of the week is a certain month.109 #Day = Get_start_day (year,month,date) the #print (' {} year {} month {} Day is week {} '. Format (year,month,date,day))111 #Print a calendar the Print_calendar (year,month,date)113 the #Execution theMain ()

Python Print Calendar

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.