How can i print random color circles in turtle?

Question:

the below code is one part of my code. I would like to print random colored circles. But it does not work. Can someone please help me to correct this code? circles shouldnt be overalapped!!
ERROR is bad color sequence: (164, 13, 120)

 from random import randint
from svg_turtle import SvgTurtle
import turtle
from turtle import Turtle
import json


def fiber_circle(fiber, width, height):
    fiber_r=25
    fiber_num = 100
    cursor_size = 20
    fiber.hideturtle()
    fiber.screen.bgcolor("white")
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    fiber.color(r,g,b)
    fiber.screen.colormode(255)
    fiber.shape("circle")
    fiber.shapesize(fiber_r / cursor_size)
    fiber.speed("fastest")
    fiber.penup()
    fibers = [] 
    
    for _ in range(fiber_num):
        fiberr = fiber.clone()
        fiberr.setposition(
            randint(-width / 2, width / 2),
            randint(-height / 2, height / 2),
        )

        
Asked By: dilara

||

Answers:

Your program, if it ran, would simply change the turtle cursor into different colors, but in one location. If you want to draw randomly colored circles all over your window, you can try something like the following which uses dot() to draw them:

from turtle import Screen, Turtle
from random import randint

FIBER_RADIUS = 25
FIBER_NUMBER = 100
CURSOR_SIZE = 20

def fiber_circle(fiber):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)

    x = randint(FIBER_RADIUS - width//2, width//2 - FIBER_RADIUS)
    y = randint(FIBER_RADIUS - height//2, height//2 - FIBER_RADIUS)

    fiber.color(r, g, b)
    fiber.goto(x, y)
    fiber.dot(FIBER_RADIUS * 2)  # dot() takes a diameter

screen = Screen()
screen.colormode(255)

width, height = screen.window_width(), screen.window_height()

fiber = Turtle()
fiber.hideturtle()
fiber.speed("fastest")
fiber.penup()

for _ in range(FIBER_NUMBER):
    fiber_circle(fiber)

screen.exitonclick()

Alternatively, you can use your original approach of shaping, sizing and coloring the turtle itself, but then use stamp() to leave behind a circle while moving the turtle randomly around the screen:

...

def fiber_circle(fiber):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)

    x = randint(FIBER_RADIUS - width//2, width//2 - FIBER_RADIUS)
    y = randint(FIBER_RADIUS - height//2, height//2 - FIBER_RADIUS)

    fiber.color(r, g, b)
    fiber.goto(x, y)
    fiber.stamp()

screen = Screen()
screen.colormode(255)

width, height = screen.window_width(), screen.window_height()

fiber = Turtle()
fiber.hideturtle()
fiber.shape('circle')
fiber.shapesize(FIBER_RADIUS * 2 / CURSOR_SIZE)  # CURSOR_SIZE is effectively a diameter
fiber.speed('fastest')
fiber.penup()

for _ in range(FIBER_NUMBER):
    fiber_circle(fiber)

...
Answered By: cdlane