pythondef函数在所有情况下都返回None
它总是返回pythondef函数在所有情况下都返回None,python,Python,它总是返回none值,但我想在结果中打印bool您的代码 def leap(year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else: return False print(leap(1992)) 让我们把它分解为1992年 年份=1992年 1992%4=0。符合事实的代码移动到下
none
值,但我想在结果中打印bool
您的代码
def leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
print(leap(1992))
让我们把它分解为1992年
1992
的输出为None
,因为您没有在这里添加任何返回false的逻辑None
输出的简单方法如下
def leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
print(leap(1992))
1) 写下代码,不要将其放在图像中2)将每个条件都放在if中(如果它输入了if,并且两个内部if中的一个为false,则没有其他条件,并且返回none)欢迎使用堆栈溢出!我建议大家复习一下关于如何最好地在这里发布问题的标准,这样可以得到有用的反馈。具体来说,您应该始终在问题中包含as代码(而不是图像),以及清晰的问题陈述。祝你好运,快乐!你的逻辑是错误的。试着想想函数在接收到您提供的输入(1992年)时的路径,让OP知道您可以用Python(和布尔逻辑)做什么。整个函数体可以如下所示:
返回bool((不是第%4年)和(第%100年)或(不是第%400年))
但逻辑仍然错误。在1992
年份试用它。对于1992
,您应该得到False
。你能详细说明一下你的建议吗?@Amit 1992是闰年,不应该返回错误。检查您的if
逻辑。我只是向他解释了为什么他的函数返回None
。我无意为他做他的工作。这样做的目的只是为了证明它为什么一无所获。
def leap(year):
if year % 4 == 0 and year % 100 == 0 and year % 400 == 0:
return True
else:
return False
print(leap(1992))