python-3.x - 为什么闰年也没有返回?
问题描述
检查提供的是否是闰年,然后使用函数以真或假的形式返回 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))
解决方案
由于您正在打印函数的结果 - 而不是打印单词 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,这就是您在输出中看到的。
推荐阅读
- linux - 如何在特定端口上打印传入的 http 请求
- reactjs - 更新具有对象数组的状态
- hive - 防止 Hive 中的“NULL”值
- javascript - 如何运行 tslint.json?
- tensorflow - Tensorflow 重新训练图像分类器
- c# - 尝试使用 NUnit 测试构造函数时在构造函数中获取有关参数的错误
- swift - 使用 Vapor 查询和映射
- c - 开发一个函数,该函数返回数组中存在的不同值的数量
- google-chrome - 使用 G Suite 帐户在 Chrome 扩展程序中进行身份验证时出现问题
- android - 如何在RelativeLayout中右侧有ImageView时使按钮的宽度匹配父级?