• bendickens8

Python tutorial 5: Creating our own commands, with a seasonal twist

If you've missed the previous coding tutorials you can catch up here:

Python tutorial 1

Python tutorial 2

Python tutorial 3

Python tutorial 4

So far in the first four tutorials we have looked at a number of topics to get us started with learning to code. We learnt about storing data using variables and repeating blocks of code using loops. In this post we will introduce another key topic in modern programming: the ability to define our own commands.

First, let us introduce our task. As it's December we are now allowed to talk about the holiday season. We will therefore write a program to draw a Christmas tree.

Let us start setting the scene. There are some new commands we haven't seen yet in the code below. Lines 3 and 4 help us set the background colour. First we store the Screen() and give it a name, in this case background. We can then use the command bgcolor which changes the background colour.

Lines 6 to 13 create the trunk of the Christmas tree. I have included a comment next to each line. These comments (anything after the # symbol) are ignored by Python. They're there for us humans.

We now would like to draw a Christmas tree by drawing three triangles. Let's create a new turtle and draw the first triangle.

We will leave it as an exercise for you to follow the steps we have added to draw the first triangle. Note that since we start from the middle, the horizontal line is drawn in two halves, the first half in the beginning and the second half at the end.

What should happen next? We need to move the tree turtle to its new position and draw another triangle. But the second triangle should be smaller. Now, the code to draw another triangle will be almost identical to the first one. We don't want to have to repeat the code. However the triangle must be smaller so the numbers in the forward commands must change.

Wouldn't it be nice if Python knew how to draw a triangle in the way we want it? Luckily, we can teach Python new commands. We call these functions and we can do so by defining the new functions using the Python keyword def. Let us re-write the code above but the code in lines 26-35 will now be packaged into a function definition:

On line 27 we are defining the new command. We chose to call it draw_triangle. Like variables, it is up to us to decide what to call the new function. We also follow the name of the new command with brackets and by putting a name in the brackets (again, it's up to us what to call this) we are telling Python that when we use this command we are going to put a number in the brackets and we want to store this number as the variable size.

Note that we see the colon and the indent appear again (remember them from the for loop). Whenever we use forward inside the function definition, we now refer to the size of the triangle using the word size.

On line 38, we now tell Python to run the new command it has just learnt, and that we want the size of the triangle to be 200. Try changing this number to see what happens.

Now here's your task. Can you move the tree turtle to its new position (make sure it's also facing the right way) and then use the new function we have created to draw a smaller triangle. And then do the same with a third even smaller triangle.

Here's how the final tree may look like:

We hope you enjoy our coding tutorials. If you'd like to take your coding to the next level why not consider one of our coding courses.

  • Facebook
  • Twitter
  • Instagram

© Codetoday Limited

Codetoday Limited is a company registered in England (company number 9789836).

Registered office: 13 Hawley Crescent, London, NW1 8NP, United Kingdom

Terms & Conditions

Privacy policy

Cookies policy