python精典习题——输入某年某月某日,判断这一天是这一年的第几天?

首先要用做数学题的思维去分析逻辑

思路:先把月份转化为天数,再加上第几天。 但,要判断是闰年还是平年

  1. 二月,平年有28天,闰年有29天。
  2. 闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)
  3. 什么是闰年:
  • 被4整除且不能被100整除的为闰年
  • 被400整除的是闰年

# 用户输入的部分
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')

你可能感兴趣的:(Python)