Try it:
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.
- Swap the order of lines 27 and 28 (
t.forward...
andt.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.
- Change the initial value for
number_of_sides
to4
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.
- 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.
KPRIDE
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.
Page 1: Intro
Page 2: The theory: learn what you need to know as fast as possible.
Page 3: Try it: try out and adapt some working python code snippets.
Page 4: Debug it: Learn how to find and fix common mistakes.
Page 5: Extend it: Choose a project idea to use your newfound python skills.
Trackbacks/Pingbacks