判断平闰年

判断某一年是平年还是闰年,闰年能被4或400整除,但不能被100整除

普通版

year=int(input('请输入年份:'))
        if year % 4 == 0 or year % 400 == 0 and year % 100 != 0 : #年份满足可整除四或四百但不能整除一百。
            print('这一年是闰年')
        else:
            print('这一年是平年')

可以加个while循环

    while 1:
        year=int(input('请输入年份:'))
        if year % 4 == 0 or year % 400 == 0 and year % 100 != 0 : #年份满足可整除四或四百但不能整除一百。
            print('这一年是闰年')
        else:
            print('这一年是平年')

函数版,方便封装调用

def nian():
    while 1:
        year=int(input('请输入年份:'))
        if year % 4 == 0 or year % 400 == 0 and year % 100 != 0 : #年份满足可整除四或四百但不能整除一百。
            print('这一年是闰年')
        else:
            print('这一年是平年')
nian()

猜你喜欢

转载自blog.csdn.net/weixin_45403025/article/details/106330023
今日推荐