【问题标题】:How do I plot points from two lists using turtle graphic programming?如何使用海龟图形编程从两个列表中绘制点?
【发布时间】:2019-04-23 18:56:52
【问题描述】:

我想使用海龟图形将点绘制到使用两个列表的图形上。

x = [1,2,3,4,5,6,7,8,9,10]
y = [1,2,3,4,5,6,7,8,9,10]

当我尝试将列表放入循环中以绘制到我自定义的海龟屏幕上时,它一直给我一个错误。我是入门级程序员,所以请不要使用高级代码。这是计算机科学课程的介绍。

import turtle

s = turtle.Screen()

t = turtle.Turtle()

s.title('Canvas')


s.setup(width = 0.5, height = 0.5, startx=0, starty=0)


s.setworldcoordinates(-2,-2,12,12)



x = [1,2,3,4,5,6,7,8,9,10]

y = [1,2,3,4,5,6,7,8,9,10]


for i in range(x,y):
    t.goto(x[0],y[0])
    t.dot()

【问题讨论】:

    标签: python plot turtle-graphics


    【解决方案1】:

    一般来说,将 x 和 y 坐标保存在不同的列表中并不是一个好主意。您必须确保每个列表具有相同数量的元素(或处理它们没有的情况)。您可以zip 将它们组合成一个列表,或者只创建一个元组列表:

    coords = [(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10)]
    for c in coords:
        t.goto(c[0], c[1])
        t.dot()
    

    但如果你真的想使用 2 个列表:

    x = [1,2,3,4,5,6,7,8,9,10]
    y = [1,2,3,4,5,6,7,8,9,10]
    for i in range(len(x)):
        t.goto(x[i], y[i])
        t.dot()
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2014-12-14
    相关资源
    最近更新 更多