pythonfunctionturtle-graphicspython-turtle

I'm trying to make the petals rotate from the color orange, yellow, to orange as a loop, but it always ends in the fourth petal as the color black..?


I'm doing an assignment on using turtles to make sunflower petals using pentagons. Everything is as it should be, except for the color of the petals. It strictly says that the colors of said pentagons have to be in a rotation of "orange, yellow, orange, yellow" with it repeating from there on. To do so we have to use functions, and since I'm generally new to computer science, I'm having trouble finding out the cause as to why it is making the fourth sunflower petal each test run as black instead of yellow.

I've tried defining the term "yellow", as "tur.pencolor('yellow') (tur is the name of the turtle in my program), and it resulted in it just not changing anything. At this point with my limited knowledge in this field, I'm stumped and are looking for help here for someone to save my skin and figure this function thing situation out for me!

Here is my code so far:

import turtle
# Define function draw_petal

def draw_petal(tur, speed, n_sided, radius):
    """
    Draws a petal of a sunflower using turtle graphics.
    
    Parameters:
    - tur (turtle.Turtle): The turtle object used for drawing.
    - speed (int): The drawing speed of the turtle. Accepts standard turtle speed inputs.
    - n_sided (int): The number of sides of the petal, indicating its shape.
    - radius (float): The radius of the petal, measured from the center to any vertex.
    
    The function draws a petal as an n-sided polygon with the specified radius. The petal shape is 
    determined by the number of sides (n_sided) and its size by the radius.
    
    Note: The turtle will start drawing from its current position and heading.
    """
    # TODO: Remove pass and provide your own code below
    angle = 360 / n_sided
    
    
    for i in range(n_sided):
        tur.forward(radius)
        tur.left(angle)
        
        
    
# Define fuction draw_sunflower
def draw_sunflower(tur, speed, n_sided, radius, num_petal):
    """
    Draws a sunflower using turtle graphics.
    
    Parameters:
    - tur (turtle.Turtle): The turtle object used for drawing.
    - speed (int): The drawing speed of the turtle. Accepts standard turtle speed inputs.
    - n_sided (int): The number of sides for each petal, indicating the shape of the petal.
    - radius (float): The radius of each petal, measured from the center to any vertex.
    - num_petal (int): The total number of petals to draw for the sunflower.
    
    The function will alternate petal colors between 'orange' and 'yellow'. The heading angle and current
    color of each petal are printed to the console during the drawing process for diagnostic purposes.
    
    Note: This function assumes a helper function `draw_petal` is defined elsewhere to draw individual petals.
    """
    # TODO: Remove pass and provide your own code below
    tur.speed(speed)
    angle_increment = 360 / num_petal
    c = tur.color()
    
    for i in range(num_petal):
        draw_petal(tur, speed, n_sided, radius)
        if(i % 2 == 0):
            tur.color("yellow")
        if(i % 2 == 1):
            tur.color("orange")
        # Diagnostic printout
        print(f"Current color: {c}, heading angle {tur.heading()} degrees")
        
        tur.left(angle_increment)

# Define the main() function for drawing sunflowers using turtle graphics.
#
# This function sets up a turtle environment and prompts the user 
# for inputs to draw a sunflower. The user can choose the animation 
# speed, the number of sides for a petal, the radius of petals, and 
# the total number of petals.
#
# After drawing a sunflower based on the user's input, the program 
# asks the user if they'd like to draw another sunflower. If the 
# user enters "yes", the screen will be cleared and the user will be 
# prompted for new input values. If the user enters "no", a message 
# will be displayed prompting the user to click on the window to exit.
#
# If the user enters any other command, an error message is printed 
# and the user is prompted again.
#
# The function concludes by waiting for the user to click on the turtle 
# window, at which point it will close the window and exit the program.

def main():
    screen = turtle.Screen()
    screen.title("Sunflower Drawing")
    
    tur = turtle.Turtle()
    
    while True:
        # User input
        speed = int(input("Enter animation speed: "))
        n_sided = int(input("Enter the number of sides of a petal: "))
        radius = float(input("Enter the radius of petals: "))
        num_petal = int(input("Enter the number of petals: "))
        
        # Draw the sunflower
        draw_sunflower(tur, speed, n_sided, radius, num_petal)
        
        # Ask if the user wants to draw another sunflower
        response = input("Draw Another Sunflower?" )
        
        if response == "yes":
            tur.clear()
            tur.penup()
            tur.home()
            tur.pendown()
        elif response == "no":
            tur.penup()
            tur.goto(0, -200)
            tur.write("Click window to exit...", align="right", font=("Courier", 12, "normal"))
            break
            turtle.exitonclick()
        else:
            print("Invalid command! Please try again…")
    
    screen.mainloop()

# Run the program
if __name__ == "__main__":
    main()

# turtle.exitonclick()

Solution

  • You just need to think about the order in which you do things - it's sequential unless told otherwise.

    Make sure that you set a colour BEFORE using it to draw a petal.

    Make sure that you update c every time the colour changes, or you will simply output "black".

    def draw_sunflower(tur, speed, n_sided, radius, num_petal):
        tur.speed(speed)
        angle_increment = 360 / num_petal
        
        for i in range(num_petal):
            if(i % 2 == 0):
                tur.color("yellow")
            if(i % 2 == 1):
                tur.color("orange")
            draw_petal(tur, speed, n_sided, radius)           # <=== NOW YOU CAN DRAW YOUR PETAL!
            # Diagnostic printout
            c = tur.color()                                   # <=== UPDATE THIS BEFORE WRITING IT OUT
            print(f"Current color: {c}, heading angle {tur.heading()} degrees")
            
            tur.left(angle_increment)