Python 当使用sys.stdout.write时,“使用”;无”;出现在我写的东西之后

Python 当使用sys.stdout.write时,“使用”;无”;出现在我写的东西之后,python,python-2.7,Python,Python 2.7,我的代码如下所示: import sys print "What are his odds of hitting?", ( 25.0 / 10.0 ) * 8 + 65, sys.stdout.write('%') 当我在Powershell(Windows 7)中运行它时,我得到以下信息: What are his odds of hitting? 85.0%None 我想得到的是: What are his odds of hitting? 85.0% 为什么我会在结尾处得到“无”?如

我的代码如下所示:

import sys
print "What are his odds of hitting?", ( 25.0 / 10.0 ) * 8 + 65, sys.stdout.write('%')
当我在Powershell(Windows 7)中运行它时,我得到以下信息:

What are his odds of hitting? 85.0%None
我想得到的是:

What are his odds of hitting? 85.0%
为什么我会在结尾处得到“无”?如何阻止这种情况发生?

sys.stdout.write(“%”)
返回
None
。它只打印消息,不返回任何内容

只需将“%”放在末尾,而不是调用
sys.stdout.write

或者,您可以在此处使用
.format()

print "What are his odds of hitting? {}%".format(( 25.0 / 10.0 ) * 8 + 65)
sys.stdout.write('%')
返回
None
。它只打印消息,不返回任何内容

只需将“%”放在末尾,而不是调用
sys.stdout.write

或者,您可以在此处使用
.format()

print "What are his odds of hitting? {}%".format(( 25.0 / 10.0 ) * 8 + 65)
您正在打印
sys.stdout.write()
调用的返回值:

print "What are his odds of hitting?", ( 25.0 / 10.0 ) * 8 + 65, sys.stdout.write('%')
该函数返回
None
。函数写入与
print
相同的文件描述符,因此您首先将
%
写入stdout,然后要求
print
stdout
写入更多文本,包括返回值
None

您可能只是想在末尾添加
%
,而没有空格。使用字符串连接或格式:

print "What are his odds of hitting?", str(( 25.0 / 10.0 ) * 8 + 65) + '%'

这两种字符串格式变体将浮点值的格式设置为小数点后两位小数。请参阅(有关
“…”%
变体,旧式字符串格式),或(有关,语言的新添加)

您正在打印
sys.stdout.write()调用的返回值。
调用:

print "What are his odds of hitting?", ( 25.0 / 10.0 ) * 8 + 65, sys.stdout.write('%')
该函数返回
None
。函数写入与
print
相同的文件描述符,因此您首先将
%
写入stdout,然后要求
print
stdout
写入更多文本,包括返回值
None

您可能只是想在末尾添加
%
,而没有空格。使用字符串连接或格式:

print "What are his odds of hitting?", str(( 25.0 / 10.0 ) * 8 + 65) + '%'

这两种字符串格式变体将浮点值的格式设置为小数点后两位小数。请参阅(有关
“…”
变体,旧式字符串格式),或(有关,语言的新添加)