我在这里没有看到问题,但 Python 的想法不同:
x = 3
y = 7
z = 2
print "I told to the Python, that the first variable is %d!" % x
print "Anyway, 2nd and 3rd variables sum is %d. :)" % y + z
I get TypeError: cannot concatenate 'str' and 'int' objects
.
为什么会这样?据我所知,我还没有将任何变量设置为字符串。
%
优先级高于+
, so s % y + z
被解析为(s % y) + z
.
If s
是一个字符串,那么s % x
是一个字符串,并且(s % y) + z
尝试添加一个字符串(结果s % y
)和一个整数(的值z
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)