【发布时间】:2022-11-21 17:20:47
【问题描述】:
我试着写这段代码:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
我得到了这个结果:
>>>
2
None
None 是从哪里来的?这是什么意思?
也可以看看
接受的答案解释了returning 函数值的重要性,而不是printing 它。有关详细信息,请参阅What is the purpose of the return statement? How is it different from printing?。
要了解 None 结果本身,请参阅 What is a 'NoneType' object?。
如果您在函数内部 printing 以查看多个值,最好改为搜集这些值,以便它们可以被调用代码打印出来。详情见How can I use `return` to get back multiple values from a loop? Can I put them in a list?。
【问题讨论】:
-
你忘了在你的函数中返回一个值所以 intrepretor 没有返回
-
@Georgy 我已经将该链接的规范编辑到问题中。最近我一直在做一些清理 Python 问题规范的工作。