我可以用这种方式在python中使用print语句嵌入变量
i=10
print "Value is %s" % (i)
Run Code Online (Sandbox Code Playgroud)
产量
Run Code Online (Sandbox Code Playgroud)Value is 10
但这样做
i=10
sys.stdout.write ("Value is %s") % (i)
Run Code Online (Sandbox Code Playgroud)
给我以下错误
Run Code Online (Sandbox Code Playgroud)TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
我可以使用sys.stdout.write而不是print来嵌入变量吗?
Sve*_*ach 16
你的括号错了.应该
i=10
sys.stdout.write("Value is %s" % i)
Run Code Online (Sandbox Code Playgroud)
在%操作者需要一个字符串和元组(或单个对象)作为参数.您试图将运算符应用于返回值sys.stdout.write(),即None.您需要在传递给字符串之前将其应用于字符串sys.stdout.write().
| 归档时间: |
|
| 查看次数: |
19439 次 |
| 最近记录: |