Python,闰年逻辑错误

Python,闰年逻辑错误,python,Python,我需要在上述代码中更改什么? 1992,2400,2000... 这对这些不起作用。 我找不到任何其他逻辑。您的代码中有很多错误,看起来您错过了%400的操作 def is_leap(year): leap = False if(year % 4 == 0 and year % 100 != 0) or (year%400 == 0 ): print 'Leap Year' else: return leap 输出 def is

我需要在上述代码中更改什么? 1992,2400,2000... 这对这些不起作用。
我找不到任何其他逻辑。

您的代码中有很多错误,看起来您错过了%400的操作

def is_leap(year): 
    leap = False 
    if(year % 4 == 0 and year % 100 != 0) or (year%400 == 0 ): 
        print 'Leap Year' 
    else: 
        return leap
输出

def is_leap(year):
    leap = False
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0 ):
        leap = True 
    return leap

print(is_leap(2000)) # for 2.7 just print is_leap(2000)
print(is_leap(1000))
print(is_leap(1992))
print(is_leap(2400))

您的函数名为
is\u leap
。这与
打印
无关。因此,不要在函数中打印

应该足够了

编辑:我在维基百科上找到了精确的定义:

每年被4整除就是闰年,除了那些被100整除的年份,但是如果这些百年被400整除就是闰年

我们的职能是:

def is_leap(year): 
    return year % 4 == 0

它是如何工作的?请看下面的回答:您是否计划在任何时候将leap设置为True?并且
year 400==0
是无效语法,因此请确保您正确地复制了它。
year=2400
对我来说是闰年,复制粘贴您的if行和下面的一行。如果它对您不起作用,您的问题就在别处。您可以通过这样做进一步简化:
def is_leap(year):return bool((year%4==0和year%100!=0)或(year%400==0))
非常感谢,它起了作用,好像问题是我刚刚打印的真的……@AChampion感谢您纠正它
def is_leap(year): 
    return year % 4 == 0
def is_leap(year): 
    return year % 4 == 0 and not (year % 100 == 0) or year % 400 == 0