Python classic exercises-input a certain year, a certain day, a certain day, determine which day is the day of the year?

First of all, we must use the thinking of math problems to analyze logic

Idea: First convert the month to the number of days, and then add the day. But, to judge whether it is a leap year or a normal year

  1. In February, there are 28 days in a normal year and 29 days in a leap year.
  2. There are 366 days in a leap year (31 days, 29 days, 31 days, 30 days, 31 days, 30 days, 31 days, 31 days, 30 days, 31 days, 30 days, 31 days from January to December)
  3. What is a leap year:
  • Leap years are divisible by 4 and not divisible by 100
  • The one divisible by 400 is a leap year

# 用户输入的部分
year = int(input('Please enter a year:\n'))
mouth = int(input('Please enter a month:\n'))
day = int(input('Please enter the number of days:\n'))

# 月份转化为天数,以平年为准
mouths = (0, 31, 59, 90, 120, 151, 181, 212, 243, 173, 304, 334)    
if 0 < mouth <= 12:
    sum1 = mouths[mouth-1]
else:
    print('Date Error !')

# 再判断是闰年还是平年
sum1 += day		# 这里sum1 就表示总天数
if(year % 400 == 0)or(year % 4 == 0)and(year % 100 != 0):
    if mouth > 2:
        sum1 += 1

print(f'it is the {sum1} day !\n')

Guess you like

Origin blog.csdn.net/qq_41076531/article/details/102159295