为leap年写一个python函数

问题描述 投票:-2回答:2
<

def is_leap(year): if year >=1900: if year%4==0: if year%100==0: if year%400==0: print ("True") else: print ("False") else: print("True") else: print("False") year = int(input()) print(is_leap(year))

python function leap-year
2个回答
1
投票
您没有指定输入,并且该功能对我来说似乎并不完整,但是在给定的示例中,当您的输入参数year小于1900(该功能的第二行)时,该功能将不会打印任何内容。

编辑:对于您现在给出的示例,这是不正确的。我没有检查仅语法的数学运算。但是请注意,您只检查1900年之后的年份。小于1900的每个输入都将不被处理,并且始终为False:

def is_leap(year): if year >=1900: if year%4==0: if year%100==0: if year%400==0: return True else: return False else: return True else: return False return False year = input() print(is_leap(year))


0
投票
您可以检查缩进的def is_leap(year):开头吗?我在is_leap(2019)上通过调用Repl.it运行了此代码,并且运行良好。从function is first class in Python开始,您的最后一次呼叫实际上将打印对象。这意味着,当您调用print(function)时,将打印函数返回的对象。由于您的函数不返回对象,因此将打印None类型。
© www.soinside.com 2019 - 2024. All rights reserved.