首页 > 解决方案 > 为什么闰年也没有返回?

问题描述

检查提供的是否是闰年,然后使用函数以真或假的形式返回 ans

enter code here
    def leapcount(inp):
if inp%4==0 or inp%400==0:
    print('True')
elif inp%100==0:
    print('False')
else:
    return ('False')

inpp=int(input('输入检查闰年格式的年份:')) print(leapcount(inpp))

标签: python-3.x

解决方案


由于您正在打印函数的结果 - 而不是打印单词 true 和 false 您可以返回布尔值,True然后False打印该结果以获得您想要的

def leapcount(inp):
  if inp%4==0 or inp%400==0:
    return True;
  elif inp%100==0:
    return False
  else:
    return False

inpp=int(input('Enter the year to check for leap year format:'))

print(leapcount(inpp)) #Prints 'True' or 'False' depending on Input Value

最初你只是在你的函数中打印而不是返回。在 python 中,没有返回值的函数将返回 None,这就是您在输出中看到的。


推荐阅读