loading...
Cover image for Teaching code reuse using Python Turtle

Teaching code reuse using Python Turtle

ducaale profile image Mohamed Dahir Updated on ・4 min read

I have been teaching some students the basics of programming using python. Our latest session involved introducing them to the concept of code reuse through python turtle. Here is how we went about it.

0. Initial Setup

We were using python IDLE to code interactively. We first began by importing everything from turtle namespace.

from turtle import *

1. Make a square manually

The first step after introducing forward, left and right functions was to draw a square.

forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)

This process gets a little bit boring for the students. However, we did this so they could appreciate the next step.

2. Use a loop for making a square.

Next, We asked the students to spot the repetition in our code. After that, they were reminded of for-loops and how they could use it to execute some piece of code repetitively.

for i in range(4):
    forward(100)
    left(180 - 90)

3. Make a square function

Next, The students were shown the circle function. After that, they were instructed to try using the square function which is not defined and therefore will produce an error. We did this so they could later understand the need for defining functions.

circle(50)
square(50)
'''
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    square()
NameError: name 'square' is not defined
'''

Next, they were told that they needed to define a square function in order to use it.

def square():
    for i in range(4):
        forward(100)
        left(180 - 90)

# now we can use our square function
square()

4. Parameterize square's length

Next, After they have used the square function, we pointed out that our function was drawing a square of fixed size. Then we showed them how they can parameterize its size.

def square(l):
    for i in range(4):
        forward(l)
        left(180 - 90)

# we can make squares of different sizes
square(50)
square(100)
square(70)

5. Parameterize the square's sides and angles and rename it into a polygon

Next, We showed them that by replacing 4 with 3 and 90 with 60, we can draw a triangle.

def square(l):
    for i in range(3):
        forward(l)
        left(180 - 60)

square(50)

Next, they were asked what would happen if they made those two values customizable?

def square(l, s, a):
    for i in range(s):
        forward(l)
        left(180 - a)

Next, we showed them that they could draw any polygon by passing the right arguments.

square(100, 4, 90)  # square
square(100, 3, 60)  # triangle
square(100, 5, 108) # pentagon
square(100, 5, 36)  # star
square(1, 360, 179) # circle

Because our function was no longer limited to just squares, we renamed it into polygon.

def polygon(l, s, a):
    for i in range(s):
        forward(l)
        left(180 - a)

6. Define a square function using the polygon function

To further solidify the concept of functions, they were instructed to define a square function using the previously defined polygon function.

def square():
    polygon(100, 4, 90)

Next, they were told to make the size customizable.

def square(l):
    polygon(l, 4, 90)

The last two code snippets were aha moments for some students as it showed them functions calling other functions.

7. Make a parallelogram manually

forward(100)
left(180 - 60)
forward(100)
left(60)
forward(100)
left(180 - 60)
forward(100)
left(60)

8. Make a parallelogram function

The next step was to define a parallelogram. But before that, they were again told to spot repetition in the code so they can turn it into a loop.

for i in range(2):
    forward(100)
    left(180 - 60)
    forward(100)
    left(60)

Next, it was turned into a function that is called a parallelogram.

def parallelogram():
    for i in range(2):
        forward(100)
        left(180 - 60)
        forward(100)
        left(60)

parallelogram()

9. Parameterize the parallelogram's angles and length

To make a parallelogram of different sizes and angles, they were instructed to parametrize them.

def parallelogram(l, a1, a2):
    for i in range(2):
        forward(l)
        left(a1)
        forward(l)
        left(a2)

parallelogram(100, 180-30, 30)

10. Make a diamond function

Next, they were instructed to make a diamond function using the parallelogram function.

def diamond(l):
    parallelogram(l, 60, 180-60)

11. Use the diamond to make a snowflake function

Next, they were instructed to use a loop for making 10 diamonds and tilt by 36 degrees after each iteration.

def snowflake(l):
    for i in range(10):
        diamond(l)
        left(36)

Note that multiplying the tilt and number of diamonds should amount to 360 degrees.

12. Make 10 snowflakes in random positions with random sizes

To do this, We added two parameters x and y to the diamond function. We also utilized the goto function for jumping to different locations. To avoid drawing lines when moving to different locations we used penup to disable drawing and pendown enable it again.

def snowflake(l, x, y):
    penup()
    goto(x, y)
    pendown()
    for i in range(10):
        diamond(l)
        left(36)

To generate a random value, we used the randint function.

from math import randint
randint(0, 100) # this will give us a random value between 0 and 100

Using randint with the snowflake function, students were able to draw 10 snowflakes with random positions in a loop.

for i in range(10):
    s = randint(50, 200)
    x = randint(-300, 300)
    y = randint(-300, 300)

    snowflake(s, x, y)

Summary

  1. Make a square manually.
  2. Use a loop for making a square.
  3. Make a square function.
  4. Parameterize square's length.
  5. Parameterize the square's sides and angles and rename it into a polygon.
  6. Define a square function using the polygon function.
  7. Make a parallelogram manually.
  8. Make a parallelogram function.
  9. Parameterize the parallelogram's angles and length.
  10. Make a diamond function.
  11. Use the diamond to make a snowflake function.
  12. Make 10 snowflakes in random positions with random sizes.

Posted on by:

Discussion

pic
Editor guide