Home > Blockchain >  In Python turtle graphics, color() can't iterate over a list of colors
In Python turtle graphics, color() can't iterate over a list of colors

Time:09-17

Starting out with turtle graphics. I wanted to create a program that prints a certain number of squares, at a certain angle apart (number of squares and angles will vary based on user input). The flow will be -

  1. User inputs the number of squares
  2. User inputs the angle between each square
  3. Each square generated should have a different color

The problem with my code is actually in step 3. I created a function with a list of colors that would populate the color() method in turtle. However, the color() method always ends up fetching the last element in my list.

from turtle import *

##Function to populate the color() method from a list of predefined colors. color(i) always populates with 'red' when I run the code.
def chooseColor():
        colorOption = ['orange', 'yellow', 'red']
        for i in colorOption:
                color(i)

#Function to create filled squares
def squareFill():
        chooseColor()
        begin_fill()
        for i in range (4):
                forward(100)
                right(90)
        end_fill()

#Function to create multiple squares
def multiSquare():
    noOfsquares = int(input("How many squares do you want to print?:\n"))
    angle = int(input("At what angle should the squares be from each other?:\n"))  
    for i in range (noOfsquares):
        squareFill()
        right(angle)

multiSquare()

I've zero clue about why color() only picks the last item in the list. Any help would be appreciated. Also, please pardon my messy code, still a noob. #TIA

CodePudding user response:

When you execute the color function your loop selects each color once for the currently drawn square and assigns that color. So each iteration of the loop overwrites the previous color making the last color in the list the selected color for every square.

You can stagger the colors by passing the count of squares that have been draw so far and taking the modulus of that number with the length of the color list to make sure it chooses a different color every time it is called.

I made some inline notes in the code below to help explain as well.

for example:

from turtle import *

##Function to populate the color() method from a list of predefined colors. color(i) always populates with 'red' when I run the code.

def chooseColor(count):  # now it will cycle through each color when called
        colorOption = ['orange', 'yellow', 'red']
        color(colorOption[count % len(colorOption)])

#Function to create filled squares
def squareFill(count): 
        chooseColor(count) # pass the incrementing number to color func
        begin_fill()
        for i in range (4):
                forward(100)
                right(90)
        end_fill()

#Function to create multiple squares
def multiSquare():
    noOfsquares = int(input("How many squares do you want to print?:\n"))
    angle = int(input("At what angle should the squares be from each other?:\n"))  
    for count in range (noOfsquares):  # take this incrementing number
        squareFill(count)   # pass it to the square builder
        right(angle)

multiSquare()

  • Related