Below you’ll see some example python code that draws some shapes
Press Ctrl + Enter to run the code.
Click on your score in the top right corner of the code editor to see what your code needs to do.
Any line of code that starts with a # is a comment: it doesn’t affect how the program runs, it’s just there to help explain what the code does.
There’s lots of code here that you probably won’t understand yet. Don’t worry: all of the challenges just involve changing the variables and constants that you see on the first 11 lines.
- Try changing the number of shapes constant to 6
You’ll see a constant called NUMBER_OF_SHAPES on line 5. See what happens when you change it.
You’ll see this constant used elsewhere in the code. This is why constants are useful – you set their value once at the top of your code and then you can use that number lots of times elsewhere in your code.
- Change the line_colour variable so that it’s always set to “red”
input("...")code on line 10 with
This means that
line_colournever changes when your code runs – it’s always set to
"red". This means we should probably change it to a constant instead of a variable…
- Change the line_colour variable into a constant called LINE_COLOUR
This is a little harder than just changing
line_colourinto capital letters on line 10 because you’ll have to change the rest of the code whenver it says
Notice that python doesn’t actually complain if your code tries to change the value of a constant (unlike other programming languages).
Putting the name of a constant in capital letters is just for your benefit (and anyone else who reads your code) to tell them that that value shouldn’t change while the code runs.
On the next page you’ll get some code examples that have been deliberately broken for you to try to fix.
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.