当前位置:网站首页>Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?

Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?

2022-07-05 08:22:00 Lazy smile

example 004: The day after that

subject : Enter the date of the year , Judge the day as the day of the year ?

  Program analysis : Calculate the day of the year , First of all, we have to calculate the total number of days in the months before this month , Add the date of the month . The sum of days in the previous months , except 2 The month is when the year is leap year 29 God , In ordinary years, it is 28 outer space , The number of days in each month is fixed , So we can set a list of days in a month , When the entered year is a leap year, put it in the list 2 The number of days in the month is changed to 29 God .

#  example 004: The day after that 
# ** subject :** Enter the date of the year , Judge the day as the day of the year ?

#  Used to judge whether the year is a leap year 
def isLeapYear(y):
    return (y % 400 == 0 or (y % 4 == 0 and y % 100 != 0))

#  Definition 1-11 Days of each month 
DofM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]

res = 0  # Calculate the day 

#  Enter the date of the year 
year = int(input('Year:'))
month = int(input('Month:'))
day = int(input('day:'))

if isLeapYear(year):  #  If it's a leap year   be 2 Month for 29 God 
    DofM[2] = 29

for i in range(month):   #  Calculation month Total previous days 
    res += DofM[i]

#  The day of the year   yes    month Total previous days + The number of days in this month day
print(res + day)

  give the result as follows :

 

原网站

版权声明
本文为[Lazy smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050816109899.html