Lesson 2.3: Turtle Stars
In the previous lesson, we wrote our first Turtle programs. For example, the program for drawing a five-point star looked like this:
from turtle import * shape('turtle') fillcolor('green') penup() setposition(-100,35) pendown() forward(200) right(144) forward(200) right(144) forward(200) right(144) forward(200) right(144) forward(200) right(144) penup() home()
The core of the program is this set of instructions:
forward(200) right(144) forward(200) right(144) forward(200) right(144) forward(200) right(144) forward(200) right(144)
If you are like most other programmers, you will immediately ask yourself a question: “We do the same thing (go forward & turn right) five times, isn’t there some better way of doing this?”
Thankfully, we can replace this entire sequence by just 3 lines:
for i in range(5): forward(100) right(144)
Try it yourself! You should get the exact same behavior as before. You may think that this is not a big deal, we just saved ourselves only 7 lines of typing. But imagine that you’d want to draw a 25-point star! Do you really want to type all 50 lines? You could just copy-and-paste the lines but still it would be too much work. And programmers are lazy by nature – they write as little code as possible (unless they are paid by the line :-D).
So let’s review the code snippet above in detail …
The first line, for i in range(5):, tells Python to do the next set of instructions 5 times. But which set of instructions? It turns out that, in Python, the indentation of each line is very important. In the for-loop case, the set of instructions being executed repeatedly are exactly those instructions that are indented.
Consider the following two programs:
The program on the left draws a 5-point star. The program on the right draws a straight line 500 steps long (and turns the turtle to the right at the very end). The only difference between them is the indentation of one single line!
Try it yourself – create a program called star5.py using the code on the left. Save it and execute it. And then modify it to look like the code on the right and execute again. Experiment with the code, changing the indentations of different lines and see what happens. Try not indenting any lines at all. Did you get an expected-an-indented-block error? How about indenting lines that should not be indented? You should get an unexpected-indent error.
- Indentations are super-important in Python programs!
- After a for-statement, the next line must be indented!
- The indented lines following a for-statement form a code block, which is executed multiple times depending on the conditions of the for-loop.
Now that we know how to use for-loops, creating a 15-point star should be relatively easy. We know that the inner angle for a 15-point star is 180° / 15 = 12°. So the outer angle; i.e. the angle at which our turtle must turn left or right, is 180° – 12° = 168°. To make the drawing a bit nicer (and faster), we hide the turtle. We also center the drawing.
Cool picture, isn’t it. And once you know about stars and for-loops, it’s quite straightforward to code.
Experiment with the code above on your own. Can you create a 9-point star? A 25-point star?
Now that we are becoming real programmers, we are also getting more and more “lazy” (or “efficient”, depends how you look at it). We don’t want to write one program for a 15-point star, then another for 9-point star, then yet another for 25-point star! Let’s combine all these programs into one!
Using Turtle Graphics, draw a star with n vertices, where n is odd.
To solve this exercise, you should create a new file generic_star_odd.py and save it in the Unit2 folder. Use two variables: n for the number of vertices the star will have and angle for the outer angle the turtle must turn to draw the star.
The theory behind drawing stars is quite complicated but, for odd number of vertices, you can use the fact that the inner angle is 180° divided by the number of vertices. And the outer angle is then 180° minus the inner angle.
Run your program for n=15 and then set n to a different value and run it again. Are you getting the expected results?
How about when n is even? Can we still draw a star with even number of vertices? Turns out that we can but only when the number of vertices is divisible by 4.
Implement a Python program that will use the Turtle Graphics module and draw a star with 2*n vertices, where n is even.
Create a new file generic_star_even.py and save it in the Unit2 folder. Use two variables: n and angle. Similarly as in Exercise 1, angle is the outer angle a turtle must turn. However, the number of vertices (and edges) when n is even is not n but rather 2*n.
Surprisingly, when n is even, the formula for the inner angle is still 180/n, but, in this case, n is just one half of the vertices! And, as usual, the outer angle is the difference between 180° and the inner angle.
Test your program with various values of n. Try n=4, n=6, n=18. Isn’t Python just awesome!
We really hope that you are having fun while learning some cool programming stuff! Look at all the impressive drawings you can create! And we are just getting started!