【问题标题】:Passing List of Colors to Turtle Module将颜色列表传递给 Turtle 模块
【发布时间】:2014-05-02 19:16:35
【问题描述】:

我有一个绘制不同颜色形状的函数。我的问题是如何传递颜色列表来绘制这个形状。

def drawCircle(color, radius):
    turtle.color(color)
    turtle.penup()
    turtle.setpos(0, -radius)
    xpos=turtle.xcor()
    ypos=turtle.ycor()
    head=turtle.heading()
    turtle.begin_fill()
    turtle.pendown()
    turtle.home()
    turtle.setpos(xpos,ypos)
    turtle.setheading(head)
    turtle.circle(radius)
    turtle.home()
    turtle.end_fill()
    turtle.penup()

colorList = ["red", "green", "blue", "black"]

drawCircle(colorList, 100)

这个函数应该是绕一个圆画圆弧或颜色:

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    好吧,您传递了一个颜色列表,但是由于turtle.color(color),您可以查看您的代码期望的1 种颜色。如果你想访问列表中的一个元素,你会做 color[i] 其中 i 是你想要的颜色的索引。

    如果你想为列表中的每种颜色做一些事情。

    for x in colors:
        Do something with x
    

    【讨论】:

    • 感谢您的输入,但我已经尝试过了,但我想要完成的工作与this 类似
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2016-08-27
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多