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.
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’.
Start by running the code and testing it with a variety of different answers to the questions.
- Swap the order of lines 27 and 28 (
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.
- Change the initial value for
If you test this by running the code and leaving the answers to the questions blank (or putting in something like
potatofor 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.
- 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
twhich 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
tbefore 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.
KPRIDE stands for Keywords, Predict, Run, Investigate, Debug and Extend and it’s a way of helping you explore and understand python code. Click on the image below for a set of KPRIDE activities for this python skill.