Need help making every petal on a flower a different color

Question:

How do make every petal on a flower a different color?

I tried doing this but it resulted in every flower petal being the same color. Im not sure how I can make every flower petal a different color.

import turtle
import random

not_done = False # guarantees the flower is completely drawn and won't be affected by subsequent button clicks

def set_up():
    """ sets up the screen with a title and adds the mouse listener"""
    wn = turtle.Screen()
    wn.bgcolor("pink")
    wn.title("Python Turtle Graphics: Flower Garden")

    turtle.delay(0)
    turtle.penup()
    turtle.setpos(-200, 250)    # write a large title
    turtle.color('green')
    turtle.write('Flower Garden', font = ('Courier', 40, 'bold'))
    turtle.onscreenclick(draw_flower)
    turtle.hideturtle()

leaf = turtle.Turtle()
stem = turtle.Turtle()

def draw_leaf(fill, r, g, b):
    """ draws 1 leaf and fills it if fill is true"""
    # todo
    leaf.hideturtle()
    turtle.colormode(255)

    for _ in range(8):
        if fill:
            leaf.begin_fill()
            leaf.fillcolor(r, g, b)

    for _ in range(45):
        leaf.fd(1)
        leaf.left(2)

    leaf.lt(90)

    for _ in range(45):
        leaf.fd(1)
        leaf.left(2)

    if fill:
        leaf.end_fill()

def draw_flower(x, y):
    """ draws a flower at (x,y)"""
    global not_done
    if not_done:    # function does not execute if still drawing
        return
    not_done = True
    # todo
    stem.penup()
    stem.setposition(x, y)
    stem.width(5)
    stem.pendown()
    stem.hideturtle()
    stem.right(90)
    stem.forward(75)
    stem.seth(0)


    fill = random.randint(0, 1)
    r = random.randrange(256)
    g = random.randrange(256)
    b = random.randrange(256)

    for _ in range(8):
        leaf.penup()
        leaf.setpos(x, y)
        leaf.pendown()
        draw_leaf(fill, r, g, b)
        leaf.rt(45)


    not_done = False # finished drawing the flower - don't add any code below

def main():
    set_up()
    turtle.shape('turtle')
    turtle.speed(0)
    turtle.done()

main()

I was told I could use a loop however I’m able to fully understand what that would look like. This is my first post here so sorry for the spam earlier.

Asked By: Maybe

||

Answers:

You need to change the color for each leaf, i.e. move the color generation into the loop:

    for _ in range(8):
        fill = random.randint(0, 1)
        r = random.randrange(256)
        g = random.randrange(256)
        b = random.randrange(256)

        leaf.penup()
        leaf.setpos(x, y)
        leaf.pendown()
        draw_leaf(fill, r, g, b)
        leaf.rt(45)
Answered By: cafce25