Turtle Time!
unexpected observations from a coding exercise
So this article is inspired from a linkedIN post:
I was doing some python exercises, looping and iterations and such..
Using a library called ‘Turtle’. It’s a teaching library where you control a little turtle that runs around your screen (as you direct them to)
Leaving their tracks… well… in their tracks.
You send simple commands like: Forward, backward, left, right etc.
Forward and backward move the turtle, while right and left turn it (in degrees)
So my problem was to make a function that could generate a 5-pointed star.
After i solved that, I wanted to make the points variable, so it could generate any number of points.
Here’s the code:
from turtle import *
#This program helps the turtle make a n-pointed star
reset()
shape ('turtle')
def square(sidelength=80, numsides = 5):
#calculate the interior angle that
#the turtle must rotate to get
#to the next star point
intangle = 360/numsides*2
for i in range(numsides) :
forward(sidelength)
right(intangle)
square(numsides=5)Here’s the results of running it for a 5, 8, and 9 point star…..
So how crazy is that? My code that I thought was awesome, only works on stars with odd numbers of points!
Another crazy thing, since I made it first for a 5-point (odd) star, didn’t “even” (pun intended) think that would be a factor??
The intrinsic conclusion that I came to is that:
My algorithm only works for starts that can be drawn without the pencil leaving the paper!
a 6 points star is basically two offset triangles.
8 points, 2 squares. etc.
I don’t know, I just found it really interesting.
AND I’m also not totally convinced that I can’t tweek it to work for even numbers as well.
Feel free to offer thoughts!!


