Search code examples
rangepython-2.5

TypeError: sequence index must be integer


I just have a problem with adding between every 3 characters a ",".

print totalpoints
points = ','.join([totalpoints[i:i+3] for i in range(0, totalpoints, 3)])

Output:

875
TypeError: sequence index must be integer

Solution

  • I don't know what you are actually trying to do. But If I am not wrong, following will solve your problem.

    >>> totalpoints = 875123123 
    >>> totalpoints = str(totalpoints)
    >>> points = ','.join([totalpoints[i:i+3] for i in range(0, len(totalpoints), 3)])
    >>> points
    '875,123,123'