Question

Kinda of a follow-up from my last question but diverse enough that it compelled me to start a new thread. I need to round the coordinates of a point made by turtle from the Python turtle module, and any way which I try to do it gives me TypeError: 'Vec2D' object does not support item assignment.

I have usedpositions: [[(-100.00,-0.00), 1], [(0.00,0.00), 2], [(100.00,0.00), 1]], where the zeroth item of every sublist is coords, and, long story short, I need to round it.

The first method I tried was

for upsublist in usedpositions:
    upsublist[0][0] = round(upsublist[0][0], 2)
    upsublist[0][1] = round(upsublist[0][1], 2)

This returned the aforementioned error, and so did

for upsublist in usedpositions:
    zeroth = round(upsublist[0][0], 2)
    first = round(upsublist[0][1], 2)
    upsublist[0][0] = zeroth
    upsublist[0][1] = first

So, I am just looking for a clean way to round the x- and y-coordinates to two decimal places. Any help appreciated.

Était-ce utile?

La solution

Vec2D is immutable; you can replace a Vec2D instance with a new vector containing rounded coordinates, but you cannot modify the original.

upsublist[0] = turtle.Vec2D(round(upsublist[0][0], 2), round(upsublist[0][1], 2))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top