#Comments import turtle import random wn = turtle.Screen() wn.colormode(255) wn.setworldcoordinates(0, 500, 500, 0) wn.bgcolor("black") def main(): num_shapes = int(input("How many shapes do you want to draw?")) max_width = int(input("What is the maximum rectangle width to be drawn? ")) max_width = max(max_width, 10) max_width = min(max_width, 400) max_height = int(input("What is the maximum rectangle height to be drawn? ")) max_height = max(max_height, 10) max_height = min(max_height, 400) fred = turtle.Turtle() #loop calling the rectangle function as many time as rectangles you need. for i in range(num_shapes): draw_rect(fred, max_width, max_height) def draw_rect(turtle, width, height): side_width = random.randint(10, width) side_height = random.randint(10, height) x_pos = random.randint(5, 500-side_width) y_pos = random.randint(5, 500-side_height) turtle.pencolor(random_color(), random_color(), random_color()) turtle.fillcolor(random_color(), random_color(), random_color()) turtle.pu() turtle.goto(x_pos, y_pos) turtle.ht() turtle.speed(0) turtle.pd() turtle.begin_fill() for i in range(2): turtle.fd(side_width) turtle.rt(90) turtle.fd(side_height) turtle.rt(90) turtle.end_fill() def random_color(): return random.randint(0, 255) main() wn.exitonclick()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question