TypeError:无法连接' str'并且'漂浮'对象 - MCEdit

时间:2014-03-27 00:57:30

标签: python typeerror mcedit

我有这个:

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

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

3 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

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

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'

答案 2 :(得分:0)

试试这个:

>>> rotValues = '[rx='+ str(rotx) + ',' + "ry=" + str(roty) +"]" 
>>> print rotValues
[rx=1.0,ry=2.0]