In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge of this article "how to achieve the perpetual calendar in python", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to achieve the perpetual calendar in python" article.
Description of the topic
A: first output the prompt statement and accept the year and month entered by the user.
B: first judge whether it is a leap year according to the year entered by the user.
C: judge the number of days of the month according to the month entered by the user.
D: use a cycle to calculate the total number of days from January 1, 1900 to the year entered by the user.
E: use a cycle to calculate the total number of days between the month entered by the user and January 1 of the year entered.
F: add the days of D and E to get the total number of days.
G: use the total number of days to calculate the number of weeks on the first day of the input month.
H: according to the value of G, format the calendar of this month!
Analysis 1. Analysis
According to the eight requirements of the topic, we can see that some functions are repeated or need to call each other, so each requirement can be encapsulated into a function with the idea of module.
2. Function
① determines whether it is a leap year function.
According to the judgment rules of a leap year, if the year is a multiple of 4, but not a multiple of 100, it is a leap year or an integer multiple of 400 is also a leap year, so the function needs to pass in a parameter (year).
# judge leap year def B (year): if year% 4 = = 0 and year% 100! = 0 or year% 400 = = 0: return True return False
The function of ② to determine the number of days of the input month
Except that February changes according to normal years and leap years, the number of days per month has been determined; so at this time, you need to call the defined function to judge the leap year to determine the number of days in February; so the function needs to pass in two parameters (month and year)
There are two tips at this time. One is to judge which month is the month. At the beginning, we use equality plus or to judge whether the condition is met. In fact, we can write the month of the big month or the small month into a list, and then judge it through in. The second is that when determining the number of days, you can first define a common value, then modify it according to the month, and finally return the common value, thus missing several lines of assignment and the returned code.
Def C (year,month): days= 31 # 31 days are mostly set to the default value if month = = 2: # February to determine whether it is a leap year if B (year): days=29 else: days=28; elif month in: # judge the small month, there are only 30 days days=30 return days
The function of ③ to calculate the total number of days from 1900 in a year
Because the number of days in a normal year is different from that in a leap year, the function to judge the leap year is called to determine the total number of days in a year.
The total number of days is equivalent to the sum of the days of each year; so the sum of the days of each year needs to be traversed to get the result; so the function needs to pass in a parameter (year)
# days from a certain year to 1900 def D (year:int): day = 0 for i in range (1900 return day): if B (I): day + = 366New year: day + = 365New year
The function of the number of days between the ④ month and January 1st
The number of days from January 1 is the sum of the days of each month. At this time, you need to determine the number of days per month, that is, you need to call the previously defined function, and what you need to know is its year to determine whether it is a leap year. So the function needs to pass in two parameters (year and month)
Iterate through the number of days per month (by calling a function), and then sum it up to get the desired result
# number of days from a month to January 1st def E (year:int,month:int): days = 0 for i in range (1 year,i month): days + = C (year,i) return days
A function for ⑤ to determine the day of the week
First of all, the sum of the days of the year distance and the number of days of the month are obtained by calling, so the function needs to pass in two parameters (month and year).
Then add the total sum to one (because what you get is the month, and when you calculate the number of days, you have to calculate the prize on the same day), and then the balance of 7 is the day of the week.
# determine what day it is def G (year:int,month:int): total_day = D (year) + E (year,month) + 1 week = total_day% 7 return week
⑥ format output Calendar function
There are two formats for output, one is Sunday on the first day and the other is Sunday on the last day
The first way: Sunday is the first day, according to the day of the week function (that is, the day function of the first day of the month), you can determine the position of the first day (that is, the position of 1).
The calendar outputs 1 to the total number of days (determined according to the month) in the corresponding position (day of the week). Since the position of the 1st has been determined, and the one in front of the 1st is empty, so traverse the output space separately, and then output the calendar.
A week corresponds to seven days, that is, line breaks are made every seven days, so you also need to define a counter.
The second way: that is, on Sunday, on the last day, the other logic is the same, except that the location of the first is different.
Originally, the position on Sunday was the first, but now it has become the last, that is, the position has been moved back by 6 digits, so you only need to add it first when calculating the position, and then ask for the remainder.
# formatted output def my_print (total:int): # iCount = 0 # print ("Day\ t one\ t two\ t three\ t five\ t six") # for i in range ((G (year, month)% 7): # print (end='\ t') # iCount + = 1 # for i in range (1, C (year, month) + 1): # print (I End='\ t') # iCount + = 1 # if iCount% 7 = 0: # print ('') iCount = 0 print ('1\ t 2\ t 3\ t 4\ t 5\ t 6\ t day\ t') for i in range ((G (year,month) + 6)% 7): print (end='\ t') iCount + = 1 for i in range (1 for i in range C (year) Month) + 1): print (I End='\ t') iCount + = 1 if iCount% 7 = 0: print ('') 3. Source code #-*-coding: utf-8-*-from datetime import dateimport calendar # def B (year): # if year/4==0 and year/400! = 0year # return True# elif year/100 = 0 and year/400 = = 0def # return True# else:# return False# to judge the leap year def B (year): if year% 4 colors 0 and year% 100! = 0 or year% 400 = 0: return True return False # def C (year:int Month:int): # days = 3 days if month in [1 days 3 year,month 5 7 def 10 12]: # days = 3 days elif month = = 2if month in B (year): # days = 29 # else:# days = 28 # days per month def C (year,month): days = 3 days 31 days in the majority Set to default if month = = 2: # February to determine whether it is a leap year if B (year): days=29 else: days=28 Elif month in [4, 6, 9, 11] # judge Xiaoyue Only 30 days days=30 return days # days from a certain year to 1900 def D (year:int): day = 0 for i in range (1900 return day): if B (I): day + = 366New year: day + = 366days def E (year:int,month:int): days= 0 for i in range (January 1st): days + = C (year I) return days # determine what day it is def G (year:int,month:int): total_day = D (year) + E (year,month) + 1 week = total_day% 7 return week # formatted output def my_print (total:int): # iCount = 0 # print ("Day\ t one\ t two\ t three\ t five\ t six") # for i in range ((G (year) Month)% 7): # print (end='\ t') # iCount + = 1 # for i in range (1, C (year, month) + 1): # print (I End='\ t') # iCount + = 1 # if iCount% 7 = 0: # print ('') iCount = 0 print ('1\ t 2\ t 3\ t 4\ t 5\ t 6\ t day\ t') for i in range ((G (year,month) + 6)% 7): print (end='\ t') iCount + = 1 for i in range (1 for i in range C (year) Month) + 1): print (iEndian'\ t') iCount + = 1 if iCount% 7 = 0: print ('') if _ _ name__ = ='_ _ main__': year = int (input ('Please enter year:') month = int (input ('Please enter month)) my_print (G (year,month)) IV. Experience
Because there are many functions, and the result of the calculation is not convenient to check the result verbally, that is to say, when the result is wrong, it is not known that the problem is that provided by the system.
From datetime import dateimport calendar
There are ways to find out the number of days and days of the week in both modules, so that you can find out which step is the problem, or you can use print to verify that a single function is correct.
The above is about the content of this article on "how to achieve the perpetual calendar in python". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.