Python 为什么在定义函数后将leap值声明为false?

Python 为什么在定义函数后将leap值声明为false?,python,python-3.x,Python,Python 3.x,我已经开始学习Python,并遇到了以下代码:这是闰年计划。 为什么要定义leap=False。输出将是true或False def is_leap(year): leap = False return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0) year = int(input()) print(is_leap(year)) 此函数中的leap变量甚至没有被使用,正如您所假设的,该行只是多余的,可以(应该!)删

我已经开始学习Python,并遇到了以下代码:这是闰年计划。 为什么要定义
leap=False
。输出将是true或False

def is_leap(year):
    leap = False
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)
year = int(input())
print(is_leap(year))

此函数中的
leap
变量甚至没有被使用,正如您所假设的,该行只是多余的,可以(应该!)删除。

我们不需要定义
leap=False
,因为它没有在代码中使用,所以您可以删除它并执行此操作

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

year = int(input())
print(is_leap(year))
编写此文件并使用
leap
的一种糟糕方法是

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

year = int(input())
print(is_leap(year))

真为你高兴!看见您刚刚开始,并且已经在示例中发现了一个错误