打印命令行参数时额外输出无 [英] Extra output none while printing an command line argument

查看:99
本文介绍了打印命令行参数时额外输出无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我学习python的第一天。所以对你们中的许多人来说这是一个白费问题。请看下面的代码:

 #!/ usr / bin / env python 

import sys

def hello(name):
name = name +'!!!!'
print'hello',name

def main():
print hello(sys.argv [1])$ ​​b

$ b if __name__ =='__main__':
main()



当我运行它时

  $ .// Python-1.py爱丽丝
hello爱丽丝!!!!
None

现在,我无法理解None 来自哪里?

解决方案

计数 print 语句在您的代码中。你会发现你在 hello 函数中打印hello alice !!!打印 hello 函数的结果。因为 hello 函数没有返回一个值(你会用 return 语句)做一个值,它会结束返回对象 None main 函数中的 print 结束打印 None

It's my day 1 of learning python. so it's a noob question for many of you. See the following code:

#!/usr/bin/env python

import sys

def hello(name):
    name = name + '!!!!'
    print 'hello', name

def main():
    print hello(sys.argv[1])


if __name__ == '__main__':
    main()

when I run it

$ ./Python-1.py alice
hello alice!!!!
None

Now, I have trouble understanding where this "None" came from?

解决方案

Count the number of print statements in your code. You'll see that you're printing "hello alice!!!" in the hello function, and printing the result of the hello function. Because the hello function doesn't return a value (which you'd do with the return statement), it ends up returning the object None. Your print inside the main function ends up printing None.

这篇关于打印命令行参数时额外输出无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆