Implementation of simple calendar based on python

  • 2020-11-20 06:09:59
  • OfStack

This article is an example of python to share the implementation of simple calendar specific code, for your reference, the specific content is as follows

First of all, logic should be made clear. The difficulty of calendar lies in how to use basic knowledge to correspond the day of the week with the corresponding date. Here, I used January 1, 1917 as The day of the week, and calculated the number of days accumulated to the month we want to query to determine the day of the first day of the query month.


#  Output calendar interface 

print("*" * 50)
print(" Welcome to use [Everyday Calendar] v2.0")

#  The year in which user input was received 
year_int = int(input(" Please enter the year :\n"))
#  Define global variables to record the total number of days 
sum = 0
if year_int >= 1917:
  month_int = int(input(" Please enter month \n"))
  for year_every in range(1917, year_int): #  Traverse from 1917 Years to the year the user entered   Used to calculate the number of days up to the year entered by the user 
    if (year_every % 4 == 0 and year_every % 100 != 0) or \
                year_every % 400 == 0: #  If it is a Swiss year 366 Every day makes every year 365 day 
      sum += 366
    else:
      sum += 365
  for month_every in range(1, month_int): #  Traversal months are used to calculate by 1 The total number of days from month to month entered by the user 
    if month_every == 4 or month_every == 6 or \
            month_every == 9 or month_every == 11:
      sum += 30
    elif month_every == 2:
      if (year_int % 1 == 0 and year_int % 100 != 0) or \
                  year_int % 400 == 0:
        sum += 29
      else:
        sum += 28
    else:
      sum += 31
  #  Definition variables are used to define the number of days per month 
  day = 0
  #  Define variables   Used to calculate the current month 1 A few days to weeks 
  weak = sum % 7

  print(" day \t1\t2\t3\t4\t5\t6")
  #  Determine how many days a month the user entered 
  if month_int == 4 or month_int == 6 or month_int == 9 or month_int == 11:
    day = 30
  elif month_int == 2:
    if (year_int % 4 == 0 and year_int % 100 != 0) or \
                year_int % 400 == 0:
      day = 29
    else:
      day = 28
  else:
    day = 31
  #  Output the specified number of Spaces to let 1 The day aligns with the day of the week 
  print("\t"*weak,end="")
  i = 1
  while i <= day: #  Iterate through the user query month 
    weakend = ((sum+i)-1)% 7
    #  If the remainder is zero 6  Line feed or output space 
    if weakend == 6:
      print("%d" %i)
    else:
      print(i,end="\t")
    i += 1
else:
  print(" The system is temporarily unavailable for maintenance 1917 Years ago ")

Related articles: