Pythonの古典的な演習-特定の年、特定の日、特定の日を入力し、その年の日を決定しますか?

まず、論理を分析するために数学の問題の考え方を使用する必要があります

アイデア:最初に月を日数に変換してから、日を追加します。しかし、うるう年なのか通常の年なのかを判断する

  1. 2月は、平年で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')

おすすめ

転載: blog.csdn.net/qq_41076531/article/details/102159295