闰年的定义不必多说:可以被4整除但不能被100整除,或者可以被400整除
这里介绍两种方法:
Way1
用判断的方式,代码如下:
year = int(input('请输入年份:')) if(year % 400 == 0): print('是闰年') elif(year % 4 == 0 and year % 100 != 0): print('是闰年') else: print('不是闰年')
Way2
引用calendar模块库,代码如下:
import calendar year = int(input('请输入年份:')) if calendar.isleap(year): print('是闰年') else: print('不是闰年')