Turtle.goto 错误
Turtle.goto error
当我尝试 运行 这个 python 代码时:
import turtle
coordiantes = ['(100, 100)', '(90, 20)', '(50, 45)']
turtle.goto(coordiantes[0])
我收到此错误消息:
TypeError: new() takes 3 positional arguments but 9 were given
这是什么意思?我如何修复我的代码,以便 turtle
转到列表中的一组坐标,而无需为 x 和 y 值制作 2 个单独的列表?
我已经尝试删除括号,但它显示相同的错误消息。
goto
是Turtle实例上的一个方法,所以需要先实例化一个Turtle实例,然后传入两个数(X,Y)给goto
方法。
from turtle import Turtle
t = Turtle()
coordiantes = [(100, 100), (90, 20), (50, 45)]
t.goto(*coordiantes[0])
如果您正在阅读文档,请注意描述 here:
Most of the examples in this section refer to a Turtle instance called
turtle.
您还需要实例化您的 turtle
:
bob = turtle.Turtle()
那么你可以使用 goto
但没有字符串(文档是 here):
coordiantes = [(100, 100), (90, 20), (50, 45)]
bob.goto(*coordiantes[0])
如果您希望 turtle
(bob) 遵循您在列表中提供的所有坐标,您可以使用 for
循环:
for i in range(len(coordinates)):
bob.goto(*coordinates[i])
当我尝试 运行 这个 python 代码时:
import turtle
coordiantes = ['(100, 100)', '(90, 20)', '(50, 45)']
turtle.goto(coordiantes[0])
我收到此错误消息:
TypeError: new() takes 3 positional arguments but 9 were given
这是什么意思?我如何修复我的代码,以便 turtle
转到列表中的一组坐标,而无需为 x 和 y 值制作 2 个单独的列表?
我已经尝试删除括号,但它显示相同的错误消息。
goto
是Turtle实例上的一个方法,所以需要先实例化一个Turtle实例,然后传入两个数(X,Y)给goto
方法。
from turtle import Turtle
t = Turtle()
coordiantes = [(100, 100), (90, 20), (50, 45)]
t.goto(*coordiantes[0])
如果您正在阅读文档,请注意描述 here:
Most of the examples in this section refer to a Turtle instance called turtle.
您还需要实例化您的 turtle
:
bob = turtle.Turtle()
那么你可以使用 goto
但没有字符串(文档是 here):
coordiantes = [(100, 100), (90, 20), (50, 45)]
bob.goto(*coordiantes[0])
如果您希望 turtle
(bob) 遵循您在列表中提供的所有坐标,您可以使用 for
循环:
for i in range(len(coordinates)):
bob.goto(*coordinates[i])