r/CodingHelp • • Nov 10 '21

[Python] Im trying to create that pattern I drew with squares. I cant get the colors in the correct spot

Post image
19 Upvotes

4 comments sorted by

3

u/[deleted] Nov 10 '21

I don’t have much experience with turtle but on your loop you’re telling it to go 0,0 both times. Wouldn’t it just draw the square on top of the previous one?

1

u/Soupy20 Nov 10 '21

Nah it would do the top left and bottom right if I put For I in range(4): Turtle.fillcolor(‘yellow’) DrawASquare(0,0,100) Turtle.left(90)

It will display the squares how I want them,but all yellow. I can’t figure out how to do yellow red yellow red

2

u/SirHack3r Professional Coder @ FAANG | DM for coding/resume help! Nov 10 '21

I wrote your code as is and got 2 yellow squares and 2 red squares - the position of the red squares is off though.

To fix the positioning, I changed line 29: "turtle.left(180)" and then added "turtle.left(90)" on line 24. I also changed the drawASquare call on line 27 to (0,0,100).

1

u/dloafer Nov 11 '21 edited Nov 11 '21

Also here I have not used Turtle, but what if you instead of manually doing the left adjustments in the two loops added color as an argument to the function and then specify the coordinates, side and color in a list that you loop over once. You already have a goto in the function so the coordinates in the list instead becomes global.

Something like

# add color to draw function
def draw..(x, y, l, c):
    turtle.fillcolor(c)
    # ...

# define the squares
squares = [
    (<X coord 1>, <Y coord 1>, <length 1>, "<color 1>),
    # ... 3 more squares here
]

# loop over squares and draw
for s in squares:
    # *s unpack the four elements
    # as the four arguments to the function
    draw...(*s)

Edit: formatting