Introduction to Programming

Activity 2: Drawing a Shape by Inputting Values

Lesson 5: Arithmetic Operators

In this activity, we will combine what we know about the Pen Extension, variables, and arithmetic operators to draw a shape based on values entered by the user. The user will decide how many sides the shape has and how long each side should be!

Remember: to draw any regular polygon, the turning angle = 360 ÷ number of sides. We can calculate this automatically using the division operator!

Setting Up

  • Open PictoBlox and create a New File.
  • Select Block Coding as the coding environment.
  • Add the Pen Extension from the Extensions menu.

Creating the Variables

  • Create a variable called Sides to store the number of sides.
  • Create a variable called Length to store the length of each side.
  • Create a variable called Angle to store the calculated turning angle.

Building the Script

Now let's build the script. You will find the blocks in the mentioned palettes:

  • When flag clicked — Events
  • Erase All — Pen
  • Go to x: (0) y: (0) — Motion
  • Ask (How many sides?) and wait — Sensing
  • Set (Sides) to (answer) — Variables
  • Ask (How long should each side be?) and wait — Sensing
  • Set (Length) to (answer) — Variables
  • Set (Angle) to (360 / Sides) — Variables & Operators
  • Pen Down — Pen
  • Repeat (Sides): Move (Length) steps, Turn (Angle) degrees — Control & Motion
  • Pen Up — Pen
Block coding script showing when flag clicked, erase all, go to x 0 y 0, ask for sides and length, calculate angle as 360 divided by sides, pen down, repeat Sides times moving Length steps and turning Angle degrees, then pen up

Running the Script

  • Click the green flag to start the program.
  • Enter the number of sides when prompted (e.g. 6 for a hexagon).
  • Enter the length of each side (e.g. 100).
  • Watch PictoBlox draw your custom shape on the Stage!
PictoBlox stage showing a hexagon drawn based on user inputs of 6 sides and 100 step length

Try These Shapes!

ShapeSidesLengthTurning Angle
Triangle3100120°
Square410090°
Pentagon510072°
Hexagon610060°
Octagon810045°
Save your file with the name Dynamic Shape before moving on to the next lesson!

Subscribe to our newsletter.

Get updates to news and events.