0

我有这个:

    rotValues = '[rx='+ rotx + ',' + "ry=" + roty +"]" 

它给了我标题中显示的错误,请帮助!

4

3 回答 3

2

这样做的另一种(也是更好的方法)是使用以下str.format方法:

>>> rotx, roty = 5.12, 6.76
>>> print '[rx={},ry={}]'.format(rotx, roty)
[rx=5.12,ry=6.76]

您还可以使用以下方法指定精度format

>>> print '[rx={0:.1f},ry={1:.2f}]'.format(rotx, roty)
[rx=5.1,ry=6.76]
于 2014-03-27T01:30:31.483 回答
0

您收到此错误是因为您试图将字符串与浮点数连接起来。作为一种强类型语言,Python 不允许这样做。因此,您必须将rotxandroty值转换为字符串,如下所示:

rotValues = '[rx='+ str(rotx) + ',' + "ry=" + str(roty) +"]"

如果您希望您的值 (rotxroty) 具有一定的小数点精度,您可以执行以下操作:

rotValues = '[rx='+ str(round(rotx,3)) + ',' + "ry=" + str(round(roty,3)) +"]"

>>> rotx = 1234.35479334
>>> str(round(rotx, 5))
'1234.35479'
于 2014-03-27T00:58:25.670 回答
0

试试这个:

>>> rotValues = '[rx='+ str(rotx) + ',' + "ry=" + str(roty) +"]" 
>>> print rotValues
[rx=1.0,ry=2.0]
于 2014-03-27T01:03:30.433 回答