Python:';Vec2D&x27;对象不支持项分配
这是我上一个问题的后续部分,但内容丰富,迫使我开始新的思路。我需要在Python turtle模块中对turtle创建的一个点的坐标进行四舍五入,我尝试的任何方法都会给出Python:';Vec2D&x27;对象不支持项分配,python,rounding,turtle-graphics,Python,Rounding,Turtle Graphics,这是我上一个问题的后续部分,但内容丰富,迫使我开始新的思路。我需要在Python turtle模块中对turtle创建的一个点的坐标进行四舍五入,我尝试的任何方法都会给出TypeError:“Vec2D”对象不支持项分配 我使用了位置:[(-100.00,-0.00),1],(0.00,0.00),2],(100.00,0.00),1],其中每个子列表的第0项是坐标,长话短说,我需要将其四舍五入 我尝试的第一种方法是 for upsublist in usedpositions: ups
TypeError:“Vec2D”对象不支持项分配
我使用了位置:[(-100.00,-0.00),1],(0.00,0.00),2],(100.00,0.00),1]
,其中每个子列表的第0项是坐标,长话短说,我需要将其四舍五入
我尝试的第一种方法是
for upsublist in usedpositions:
upsublist[0][0] = round(upsublist[0][0], 2)
upsublist[0][1] = round(upsublist[0][1], 2)
这返回了前面提到的错误,也返回了前面提到的错误
for upsublist in usedpositions:
zeroth = round(upsublist[0][0], 2)
first = round(upsublist[0][1], 2)
upsublist[0][0] = zeroth
upsublist[0][1] = first
所以,我只是在寻找一种干净的方法,将x坐标和y坐标四舍五入到小数点后两位。谢谢你的帮助 Vec2D
是不可变的;您可以将Vec2D
实例替换为包含舍入坐标的新向量,但不能修改原始向量
upsublist[0] = turtle.Vec2D(round(upsublist[0][0], 2), round(upsublist[0][1], 2))
意识到这是一段时间后,但你最初的答案是非常翔实的。后来我尝试在其他地方使用同样的技术,使用了一些冗余的search=[turtle.pos](新行)search=[turtle.Vec2D(round(search[0],2),round(search[1],2))]
,但结果是TypeError:type函数没有定义\uuuuuuu round\uu方法。谢谢你的帮助。