python判断闰年


闰年的判断条件:

  1. 能被4整除,并且不能被100整除
  2. 能被400整除的数
    注意:以上两个条件满足一个即可

1.世纪闰年和普通闰年

year = int(input('请输入一个年份:'))
if(year % 4 == 0):
    if(year % 100 == 0):
        if(year % 400 == 0):
            print('{0}是世纪闰年'.format(year)) 
        else:
            print('{0}不是闰年'.format(year))
    else:
        print('{0}是普通闰年'.format(year))     
else:
    print('{0}不是闰年'.format(year))

2.是否为闰年

year = int(input('请输入一个年份:'))
if(year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0):
    print('{0}是闰年'.format(year))
else:
    print('{0}不是闰年'.format(year))

3.是否为闰年

year = int(input('请输入一个年份:'))
if year % 4 == 0 and year % 100 != 0:
    print('{0}是普通闰年'.format(year))
elif year % 400 == 0:
    print('{0}是世纪闰年'.format(year))
else:
    print('{0}不是闰年'.format(year))

4. 判断输入的日期是今天的第几天

1. 列表加循环

year = int(input('请输入年份'))
month = int(input('请输入月份'))
day = int(input('请输入今天的日期'))
date = [31,29,31,30,31,30,31,31,30,31,30,31]
count = day # 这个月一共有多少天
if(year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0):
    print('{0}是闰年'.format(year))
    date[1] = 29
else:
    print('{0}不是闰年'.format(year))
    date[1] = 28

for i in range(month - 1):
    count += date[i]
x = date[month-1]
print('本月一共有{0}天'.format(x))
print('{0}年{1}月{2}日是今年的第{3}天'.format(year,month,day,count))

2. 元组

year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入当前日期:"))
sum_day = 0  # 天数
leap_year = 0  # 闰年(闰年将year设为1)

# 使用元组定义天数
# 如果输入的月份是1月份,则直接计算day即可
# 如果输入的月份是2月份,则要先计算出1月份的天数,即为31天
# 以此类推
# 1  2   3   4   5    6    7    8    9    10   11   12
# 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
# 1. 据输入的月份,计算出前几个月的天数
if 0 < month <= 12:
    sum_day = months[month - 1]
else:
    print("输入的月份有误")
    
# 2. 判断是否为闰年:
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
    leap_year = 1

# 3. 判断如果是闰年并且输入的月份大于2则在总的天数上加1
if leap_year == 1 and month > 2:
    sum_day += day + 1
else:
    sum_day += day

print(f"这一天是这一年的第 {sum_day} 天")

3. 列表

year=int(input("年:\n"))
month=int(input("月:\n"))
day=int(input("日:\n"))
months1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年
months2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年

if ((year%4==0)and(year%100!=0)) or((year%100==0)and(year%400==0)):
    Dth=months1[month-1]+day
else:
    Dth=months2[month-1]+day
print("是该年的第%d天"%Dth)

4. time模块

import time

try:
    a=input('请输入日期(格式:xxxx-xx-xx):')
    b=time.strptime(a,'%Y-%m-%d') #时间字符串转化为元组
    # print(b)
except ValueError:
    print('请输入正确的日期格式!')
else:
    b=time.strptime(a,'%Y-%m-%d') #时间元组格式化输出
    # print(b)
    d=time.strftime('%j',b)  # %j 一年内的一天(001-366)
    # print(dd)
    y=time.strftime('%Y',b)  # %Y 四位数的年份表示(000-9999)
    print('输入的日期是%s年的第%s天'%(y,d))
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值