为什么在输出中打印“无"? [英] Why is this printing 'None' in the output?
本文介绍了为什么在输出中打印“无"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义了一个函数如下:
def 歌词():打印第一行"打印歌词()
但是为什么输出返回None
:
第一行没有任何
解决方案
因为有两个打印语句.第一个是内部功能,第二个是外部功能.当函数不返回任何内容时,它隐式返回 None
.
在函数末尾使用return
语句返回值.
例如:
返回无
.
使用return语句
<预><代码>>>>定义测试():...返回ACV"...>>>打印测试()ACV>>>>>>a = 测试()>>>打印一个ACV>>>I have defined a function as follows:
def lyrics():
print "The very first line"
print lyrics()
However why does the output return None
:
The very first line
None
解决方案
Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None
.
Use return
statement at end of function to return value.
e.g.:
Return None
.
>>> def test1():
... print "In function."
...
>>> a = test1()
In function.
>>> print a
None
>>>
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>>
Use return statement
>>> def test():
... return "ACV"
...
>>> print test()
ACV
>>>
>>> a = test()
>>> print a
ACV
>>>
这篇关于为什么在输出中打印“无"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文