Rick Weinberg
Rick Weinberg

Reputation: 13

How do I "hold" the randomly generated color?

random_turtle_color borrowed from this page.

import turtle, random
r = lambda: random.randint(0,255)
print('#%02X%02X%02X' % (r(),r(),r()))

turtle.width(10) #What does this line do?
length = 5
for count in range(100):
    colors = (z) #I want the randomly generated hexadecimal color to be #the pen in the drawing
    turtle.speed(0)
    turtle.forward(length)
    turtle.right(135)
    length = length + 5

Upvotes: 1

Views: 434

Answers (4)

Pedro Lobito
Pedro Lobito

Reputation: 99041

Your question led me to faker, a nice package to generate random data, including random colors, i.e.:

import random
from faker import Faker
length = 1
for count in range(800):
    turtle.width(random.randint(2, 15))
    turtle.speed(200)
    turtle.forward(length)
    turtle.right(135)
    turtle.left(2)
    turtle.color(Faker().hex_color())
    length = length + 15

Note: turtle.width() - defines the size of the brush


enter image description here

Upvotes: 1

user7711283
user7711283

Reputation:

For some explanations see comments in the code below:

import turtle, random
r = lambda: random.randint(0,255)
z = '#{:02X}{:02X}{:02X}'.format(r(),r(),r())
print(z)
length = 5
turtle.pen(fillcolor=z,pencolor=z,pensize=1) # pensize= width of drawed line (here 1 pixel) 
turtle.begin_fill() # Called just before drawing a shape which is to be filled.
for _ in range(100): # without '_' unused numbers are generated
    turtle.speed(0)
    turtle.forward(length)
    turtle.right(135)
    length = length + 5
turtle.end_fill() # It's question of taste, but I like the filled figure better ...
turtle.done()     # the turtle window will stay open 

gives:

enter image description here

Upvotes: 1

cdlane
cdlane

Reputation: 41925

A key piece you're missing is telling the turtle to use the color you generated via turtle.pencolor(). Here's a reworked example:

import turtle
import random

color_values = [random.randrange(0, 256) for _ in 'rgb']
hex_string = '#{:02X}{:02X}{:02X}'.format(*color_values)

turtle.speed('fastest')
turtle.pencolor(hex_string)  # Set the pen color

turtle.width(10)  # Width in pixels of the lines drawn (constant)

length = 5  # Length in pixels of the lines drawn (grows)

for _ in range(100):
    turtle.forward(length)
    turtle.right(135)
    length += 5

turtle.hideturtle()
turtle.done()

You don't need to use a hex string to set the color, you can pass color values directly if they match the color mode. Here's a rework that does that and changes the colors during the loop as well:

import turtle
import random

turtle.colormode(255)
turtle.speed('fastest')

turtle.width(10)  # Width in pixels of the lines drawn (constant)

length = 5  # Length in pixels of the lines drawn (grows)

for _ in range(100):
    color_values = [random.randrange(0, 256) for _ in 'rgb']
    turtle.pencolor(color_values)  # Set the pen color

    turtle.forward(length)
    turtle.right(135)
    length += 5

turtle.hideturtle()
turtle.done()

OUTPUT

enter image description here

Upvotes: 1

mitoRibo
mitoRibo

Reputation: 4548

Use ''.format() instead of print(). If you want to get a different color each time move the z assignment line into the for loop:

import turtle, random
r = lambda: random.randint(0,255)
z = '#{:02X}{:02X}{:02X}'.format(r(),r(),r())

turtle.width(10) #What does this line do?
length = 5
for count in range(100):
    colors = (z) #I want the randomly generated hexadecimal color to be #the pen in the drawing
    turtle.speed(0)
    turtle.forward(length)
    turtle.right(135)
    length = length + 5

Upvotes: 0

Related Questions