18: Parameters in python

Try it:

Parameters: Adapt and reuse sections of your code

Below you’ll see some python code which will draw some coloured shapes :

Press Ctrl + Enter  to run the code.

Here’s how the code works:

Lines 6-10 defines a procedure called draw_shape which has two parameters:
sides: how many sides the shape has, e.g. 4 for a square
size: the length of each side in pixels

Lines 12 – 16 defines a procedures called fill_shape. This calls draw_shape on line 15 passing on the values of the parameters sides and shape but it also has another parameter for the colour of the shape

Lines 19 and 22 call the procedure fill_shape to draw a square and triangle

Challenges:

Start by running the code.

  1. Change the size of the red square to 200

    You will need to change the value of the second parameter on line 19

  2. Draw a yellow 5 sided shape (pentagon) size 100

    You’ll need to call fill_shape again with your own values for sides, `size` and colour

  3. Draw a pink shape with 32 sides size 2

    Apparently a 32 sided shape is a triacontadigon. Yes, I had to look that up. But it looks pretty much like a circle to me when it’s that small.

On the next page you’ll get some code with both syntax and logic errors

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.

Leave a Reply