Control Flow: Conditional (while
) Loops
Random Shapes
import turtle
import random
def square(size):
for _ in range(4):
turtle.fd(size)
turtle.left(90)
def triangle(size):
for _ in range(3):
turtle.fd(size)
turtle.left(120)
turtle.pensize(3)
turtle.speed(10)
turtle.colormode(255)
min_size = 50
max_size = 100
while -350 < turtle.xcor() < 350 and -350 < turtle.ycor() < 350 :
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
turtle.pencolor(red, green, blue)
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
turtle.fillcolor(red, green, blue)
choice = random.randint(0, 2)
print(choice, " (", turtle.xcor(), ",", turtle.ycor(), ")")
turtle.begin_fill()
if (choice == 0):
square(random.randint(min_size, max_size))
elif (choice == 1):
triangle(random.randint(min_size, max_size))
elif (choice == 2):
turtle.circle(random.randint(min_size, max_size))
turtle.end_fill()
turtle.left(random.randint(0, 360))
turtle.penup()
turtle.fd(random.randint(30, 100))
turtle.pendown()
turtle.done()