python-3.x 我正在进行一个项目,试图创建一个闰年生成器,除了最后两位数为16或20的年份之外,每年都可以工作

odopli94  于 2023-02-17  发布在  Python
关注(0)|答案(3)|浏览(159)

每当我键入以16或20结尾的年份时,例如:2016年或3020年,它刚刚结束。我正在使用的代码如下,任何帮助将不胜感激。谢谢。

year = int(input("Year??? ")

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("Leap year.")
        elif year != 0:
            print("Not leap year.")
else:
    print("Not leap year.")
cngwdvgl

cngwdvgl1#

试试这样的方法:

year = int(input("Year???"))
if (year%400 == 0):
          print("Not a leap year")
elif (year%100 == 0): # elif is preferable to nested if in most cases
          print("Not a leap year")
elif (year%4 == 0):
          print("This is a leap year!")
else:
          print("This is not a leap year")
wwodge7n

wwodge7n2#

year = int(input("Year???"))

if not year % 4 and (year % 100 or not year % 400):
    print("This is a leap year!")
else:
    print("Not a leap year")
e4eetjau

e4eetjau3#

year = int(input())
if (year % 4)==0 and (year % 100)>0:
    print('it is a leapyear')
elif (year % 400) ==0:
    print('it is a leapyear')
else:
    print('it is not a leapyear')

相关问题