Python Turtle Random Rectangle

  python, python-turtle, turtle-graphics

I need some help getting the code to function properly. It should be making 10 random rectangles in 10 random spots in 10 random colors. Please don’t change any variables or anything like that. I really just struggling with getting the loop logic to function properly Here’s what I have so far:

import turtle
import random

screen = turtle.Screen()
bob = turtle.Turtle()

screen.setup(800,800)
screen.colormode(255)

bob.speed(8)

##Shape size
size =random.randint(20,100)

##Colors
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)

##X coordinate
x_co = random.randint(0,500)

##Y Coordinate
y_co = random.randint(0,500)


bob.penup()
bob.goto(x_co,y_co)
bob.pendown()
bob.fillcolor(red,green,blue)
bob.begin_fill()

##Nested loop to draw
for i in range(10):

    bob.penup()
    bob.goto(x_co,y_co)
    for i in range(4):
        bob.pendown()
        bob.fillcolor(red,green,blue)
        bob.begin_fill()
        bob.forward(size)
        bob.right(90)
        bob.end_fill()
    bob.penup()
    bob.goto(x_co,y_co)

Thank you very much

Source: Python Questions

LEAVE A COMMENT