如何判断一年是否是闰年? [英] How to determine whether a year is a leap year?

查看:84
本文介绍了如何判断一年是否是闰年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的计算器来确定某一年是否为闰年.

I am trying to make a simple calculator to determine whether or not a certain year is a leap year.

根据定义,闰年可以被四整除,但不能被一百整除,除非它可以被四百整除.

By definition, a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred.

这是我的代码:

def leapyr(n):
    if n%4==0 and n%100!=0:
        if n%400==0:
            print(n, "is a leap year.")
    elif n%4!=0:
        print(n, "is not a leap year.")
print(leapyr(1900))

当我在 Python IDLE 中尝试此操作时,模块返回 None.我很确定我应该得到 1900 是闰年.

When I try this inside the Python IDLE, the module returns None. I am pretty sure that I should get 1900 is a leap year.

推荐答案

使用 calendar.isleap:

import calendar
print(calendar.isleap(1900))

这篇关于如何判断一年是否是闰年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆