Interested in getting your child into computer programming but unsure of where to start? You’re not alone. Finding simple programming project ideas that interest pre-teens and teenagers can be tricky, especially when learning a programming language for the first time. If your child has any interest in Harry Potter, this short tutorial will introduce them to programming with a simple, fun project. We will use Python, a popular programming language, to draw a symbol any Harry Potter fan will recognize: the Deathly Hallows. We'll walk you through all of the steps below!
Programs can be written using many different software tools. Fortunately, one of the simplest to set up is Repl.it, which allows us write, run, and save our code in the browser. Start by signing up for a repl.it account.
Now that you're logged in, create your project by clicking on the +new repl button at the top of the page:
Choose the programming language Python (with Turtle) and name your project Deathly Hallows. Now, we're ready to start writing some code!
For this project, we are going to use the Python module
turtle. In Python, modules are like add-on tools we use to give our programs extra functionality. In this project, we will use
turtle to help us draw graphics on the screen.
When we use
turtle in Python, we create a Turtle object on our screen. Then, we issue commands in our code to the Turtle, by telling it to move, turn, and change color.
Thus, at the beginning of our code, we need to import the
turtle module, and we need to create the Turtle object we will use. The Turtle can have any name, like
bob. The name of the Turtle is called a variable, and variables in programming are always one word (no spaces) and usually start with a lowercase letter. Here is the code we start with to accomplish these two things:
import turtle bob = turtle.Turtle()
Now, we can try giving our Turtle a command. Let's try telling
bob to move forward 200 steps. This is how we do that:
Try running your code. There's a line on your screen! We just gave
bob the command to move forward by 200 pixels. Wherever the Turtle moves, it drags a pen behind it, leaving a line. In programming, when we issue commands like this, we are calling a function.
Let's say we want to have
bob draw a square. The next thing we need to do is to turn left by 90 degrees. Following the same pattern as before, our code should now look like this:
Notice that the Turtle's head is now pointing down, as it is ready to move forward in a new direction! How do you think we would complete the code to draw a square?
bob.forward(200) bob.left(90) bob.forward(200) bob.left(90) bob.forward(200) bob.left(90) bob.forward(200) bob.left(90)
Notice that our code is getting repetitive. We like to avoid making our programs repetitive, because it makes them more prone to small mistakes (what if one of the numbers had been written incorrectly?). Luckily, it is very easy to repeat code in programming by using loops.
What specific code do we need to repeat here? The Turtle is going forward and turning right four times total. Thus, we need to repeat those two lines of code four times.
In Python, we use a for loop to repeat certain lines of code a certain number of times. To tell the computer we're about to repeat some code, we start with the line
for i in range(4):. The number
4 represents the number of times the code inside the loop is going to repeat, and it's important to remember the colon at the end of the line. The code that goes inside the loop has to be tabbed or indented, so that the computer knows what to repeat, like this:
for i in range(4): bob.forward(200) bob.left(90)
Now we have an efficient program to draw a square! To make sure we really understand our code, let's try to modify it to draw a triangle with three equal sides. Experiment with your code to get it right!
Hint: How many times do we need to repeat our code? How many sides does a triangle have? If you were walking in a triangular shape, how much would you need to turn at every corner?
Keep experimenting until you land on something like this:
for i in range(3): bob.forward(200) bob.left(120)
You've successfully created the first shape in our Deathly Hallows symbol!
Next, we will draw the circle. Let's move
bob to the middle of the bottom side of the triangle, where we will start drawing the circle. Write a line of code that tells the Turtle to move forward just slightly less than half of the length of your triangle side. For example, if each side of our triangle is 200 pixels, then we would write below our existing code:
To draw the circle, we need to think of a circle as a shape with many very short sides. The steps are then similar to how we drew our square and triangle: draw a line, turn, draw a line, turn, etc. However, this time we need to cover 360 degrees with many more shorter sides. To make the math simple, let's repeat our code 36 times, turning 10 degrees each time. You can experiment with how far the Turtle moves forward each repetition to make the perfectly sized circle for your graphic:
for i in range(36): bob.forward(10) bob.left(10)
We're almost there! The last piece we're missing is the vertical line to the top. You should be able to experiment with our commands to move forward and turn to put this last line in place.
Hint: We need to move forward a tiny bit to put our Turtle right in the center of the bottom side of our triangle, turn the head to face up, and move forward to draw the vertical line. After some experimenting, our code to do so looks like this:
bob.forward(6) bob.left(90) bob.forward(172)
And here's our result!
The entire program is in full at the bottom of this post.
During our classes, our students work with one of our Juni instructors, from a top Computer Science university, to develop their coding skills. They work one-on-one through our project-based course curriculum, building on fundamental programming concepts and developing logical thinking and problem solving strategies.
Admission to our program starts with a free trial assessment to place each student into the correct starting level.
Here is the code to draw the Deathly Hallows symbol in Python in full:
import turtle bob = turtle.Turtle() bob.speed(1000) for i in range(3): bob.forward(200) bob.left(120) bob.forward(95) for i in range(36): bob.forward(10) bob.left(10) bob.forward(6) bob.left(90) bob.forward(172)
If you have any questions or feedback about this project, feel free to send us an email at firstname.lastname@example.org. Happy coding!
Bianca Dennis is an instructor at Juni Learning and holds a B.A. in South Asian and African Studies from Columbia University. She has previously been a Teaching Assistant at the New York Code + Design Academy, and she is currently Front-End Software Engineer at SCOIR in Philadelphia, PA.