欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python分支语句学习之判断一个日期的所在月份有多少天?(题解)

程序员文章站 2022-09-15 22:09:44
判断一个日期的所在月份有多少天? # coding = utf8 date = '2000-02-08' s = date.split('...

判断一个日期的所在月份有多少天?

# coding = utf8

date = '2000-02-08'

s = date.split('-')
year = int(s[0])
month = int(s[1])
day = int(s[2])

# print(year)
# print(month)
# print(day)
days = 0;

if month in (1, 3, 5, 7, 8, 10, 12):
    days = 31
elif month in (4, 6, 9, 11):
    days = 30
elif month == 2:
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        days = 29
    else:
        days = 28
else:
    print("出错")

print(date+'当月天数为'+str(days))