我有下面的代码将文本添加到前面 - Python的
Test1 = Price[Product.index(TestABC)]+ AddQTYPriceA
print Test1
print "try this test" + Test1
当需要打印测试1它给出了正确的答案。我想尝试添加文本到它的前面,所以我已经输入打印“尝试这个测试”+ Test1
对于第二个打印命令,它给出了以下错误。
Traceback (most recent call last):
File "C:\Python26\data.py", line 78, in <module>
print "try this test" + Test1
TypeError: cannot concatenate 'str' and 'float' objects
有人能帮助我怎样才能文字出现在面前。
Greggy d
'Test1'是一个浮点值,和Python不会自动将其转换为字符串。如果你想将它与一个字符串连接起来,你必须用'str()'''''来表示它。 –
注意[PEP-8](http://www.python.org/dev/peps/pep-0008/)建议保留'CapWords'表示类,并且使用'lowercase_with_underscores'表示局部变量,这会让你的代码更清晰。 –