使用Python绘制随机颜色的方块

-3 投票
3 回答
7033 浏览
提问于 2025-04-17 21:33

我需要每个新的小方块都有一个新的随机颜色。现在它只在循环结束后才改变颜色。你能帮我解决这个问题吗?

def random_color():
    colvar=random.randint(0,10)
    L=['red','blue','green','yellow','black','pink','gold','violet','coral','lemon chiffon','sea green'] #wiki.tcl.tk/16166  website which I used to find names for colors
    result=L[colvar]
    return result

def square(color,x,y):     
     turtle.color(color)
     turtle.begin_fill()
     turtle.penup()
     turtle.goto(x,y)
     turtle.pendown()
     line1=200           #creates a new smaller square
     while line1>=10:   
      line1=line1-10
      for i in range(2):
          turtle.forward(line1)
          turtle.right(90)
          turtle.forward(line1)
          turtle.right(90)

def drawsqr():
    num=5               
    for i in range(num):  
              color=random_color()  #change color after each complete cycle
              x=250
              y=250
              square(color,x,y)

3 个回答

-1

在编程中,有时候我们会遇到一些问题,可能是因为代码写得不够好,或者是我们对某些概念理解得不够透彻。比如说,有人可能在使用某个函数时,发现它的表现和预期不一样。这种情况很常见,尤其是对刚开始学习编程的人来说。

解决这些问题的第一步是要仔细阅读错误信息。错误信息就像是程序在告诉你哪里出了问题。虽然有时候这些信息看起来很复杂,但其实它们提供了很多有用的线索。

另外,查阅相关的文档和资料也是很重要的。很多时候,官方文档会详细解释某个函数的用法和注意事项,帮助你更好地理解如何使用它。

最后,和其他程序员交流也是一个不错的选择。你可以在论坛上发帖,或者在社交媒体上询问,很多人愿意分享他们的经验和解决方案。

总之,遇到问题时不要慌张,慢慢分析,寻找解决办法,编程的过程就是不断学习和解决问题的过程。

imputturtlegraphics,
setpcolor:("red,green,blue,gold,puple,maroon,claret,tuquoise etc")
,pendown
,goto("x$y$")
,movehundredstepsforward
,turnleft90
repeatprev2sX4
1

我知道你解决了你的问题,但在你的函数中,使用 random.choice() 可能不太合适:

我不同意。使用 random.choice() 的问题在于,你可能会在连续调用时得到相同的颜色,而这正是你不想要的:

>>> import random
>>> COLORS = ['red', 'blue', 'green', 'yellow', 'black', 'pink', 'gold', 'violet', 'orange', 'magenta', 'cyan']
>>> for _ in range(10):
...     print(random.choice(COLORS))
... 
green
pink
red
black
violet
orange
orange
violet
yellow
yellow
>>> 

而使用 random.shuffle() 配合 itertools.cycle() 可以给你一个随机的颜色序列,这个序列会重复,你可以从中一个接一个地选择不同的颜色:

import turtle
import random
import itertools

COLORS = ['red', 'blue', 'green', 'yellow', 'black', 'pink', 'gold', 'violet', 'orange', 'magenta', 'cyan']

def random_color(iterator=[]):  # intentional dangerous default value
    if not iterator:  # empty container
        colors = COLORS
        random.shuffle(colors)
        iterator.append(itertools.cycle(colors))

    return next(iterator[0])

def square(length, x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    while length >= 10:
        color = random_color()  # change color after each square
        turtle.color(color)

        turtle.begin_fill()

        for _ in range(4):
            turtle.forward(length)
            turtle.right(90)

        turtle.end_fill()

        length -= 10

square(200, -100, 100)

turtle.done()

在这里输入图片描述

-1

我知道你已经解决了你的问题,不过在你的函数中,使用random.choice()会更合适:

def random_color():
    L=['red','blue','green','yellow','black','pink','gold','violet','coral','lemon', 'chiffon','seagreen'] 
    return random.choice(L)

撰写回答