08: Sequence in Python

Try it:

Plan the order of instructions carefully
Sequence in python: Plan the order of instructions carefully

Below you’ll see some example python code that will draw a shape with any number of sides, in any colour that the user asks for.

Press Ctrl + Enter  to run the code.

Notice how the code is split up into sections using empty lines, with a comment explaining the sequence of instructions above each ‘paragraph’.

Challenges:

Start by running the code and testing it with a variety of different answers to the questions.

  1. Swap the order of lines 27 and 28 (t.forward... and t.left...)

    Hopefully, nothing will break. Your shapes will still draw. They’ll be in a slightly different place but otherwise the program will do exactly the same thing regardless of the order of these two lines.

    This shows that sequence (the order of instructions) isn’t always essential to get ‘right’: there’s more than one way to solve most problems.

  2. Change the initial value for number_of_sides to 4

    If you test this by running the code and leaving the answers to the questions blank (or putting in something like potato for the number of sides), hopefully the program will still be able to cope and carry on drawing the shape.

    This shows how setting initial values at the start of your sequence of instructions can be really helpful to avoid errors later.

  3. Try moving lines 3-5 all the way to the bottom

    This is like the opposite of challenge 1. This time the sequence (order) of instructions is really important. Lines 3-5 create a variable called t which is used to do all the drawing.

    Try running the code like this. You’ll get an error if you attempt to use the variable called t before you’ve created it.

On the next page you’ll get some code without any syntax errors, but that still needs debugging because the instructions are in the wrong order.

One thought on “08: Sequence in Python

Leave a Reply

Your email address will not be published. Required fields are marked *