Why is this printing 'None' in the output? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-17 21:23:35

问题


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

回答1:


Because there are two print statements. First is inside function and second is outside function. When function not return any thing that time it return None value.

Use return statement at end of function to return value.

e.g.:

Return None value.

>>> 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
>>> 



回答2:


Because of double print function. I suggest you to use return instead of print inside the function definition.

def lyrics():
    return "The very first line"
print lyrics()

OR

def lyrics():
    print "The very first line"
lyrics()


来源:https://stackoverflow.com/questions/59256957/qpushbutton-set-button-text-from-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!