【问题标题】:How to Change color while in a repeat in turtle如何在海龟重复中更改颜色
【发布时间】:2020-07-27 01:00:57
【问题描述】:

我已经设置好我的姓名首字母重复的过程正常工作,但是,我需要在每次重复后改变颜色。 (我知道之前有人问过这个问题,我只是不明白答案)

import turtle 
s = turtle.Turtle()
s.color("purple")



def Square(turtle):
for i in range(4):
    s.left(90)
    s.forward(150)
 s.right(20)


for i in range(16):
Square(s.right(20))
Square(s.right(10))
Square(s.right(20))

【问题讨论】:

    标签: python colors repeat turtle-graphics


    【解决方案1】:
    import turtle 
    s = turtle.Turtle()
    
    #Create list of colors you want
    colors = ["red", "green", "blue", "orange", "pink", "yellow"]
    
    def Square(turtle):
        for i in range(4):
    
            #Choosing color from list- this option is repeating colors at list from first to last 
            color_number = i % len(colors)
            current_color = colors[color_number] 
    
            #Here you set color you want
            s.color(current_color)
    
            s.left(90)
            s.forward(150)
            s.right(20)
    
    for i in range(16):
        Square(s.right(20))
        Square(s.right(10))
        Square(s.right(20))
    

    【讨论】:

    • 您好 MatoX_svk,感谢您提供堆栈溢出的答案,请尝试发布描述而不是直接提供代码解决方案,即描述 + 代码
    猜你喜欢
    • 2017-06-06
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多