Search code examples
pythontypeerrormcedit

TypeError: cannot concatenate 'str' and 'float' objects - MCEdit


I have this:

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

It gives me the error shown in the title, help please!


Solution

  • Another (and much better way) of doing this is to use the str.format method:

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

    You can also specify the precision using format:

    >>> print '[rx={0:.1f},ry={1:.2f}]'.format(rotx, roty)
    [rx=5.1,ry=6.76]