Lesson 2: Repeating Actions
Now that the VR Robot has drawn a square on the Art Canvas Playground using the Pen, you will learn to repeat blocks and simplify your project. This Lesson will walk you through the steps to draw a square on the Art Canvas Playground using the [Repeat] block.
Learning Outcomes
- Identify how to use the [Repeat] block to repeat the blocks inside of it for a set number of times.
- Identify that the [Repeat] block can accept integers as parameters.
- Identify that conditions are used to end a [Repeat] block.
- Describe why a [Repeat] block would be used in a project.
- Describe the flow of a project that contains a [Repeat] block.
Rename the Project
- If the previous Lesson’s project is not already loaded, load the Unit3Lesson1 project.
-
Rename the project by selecting the project name box.
-
Enter the new project name Unit3Lesson2, and select “Save.”
Repeating Actions
The [Repeat] block is used to repeat the blocks within it a set number of times. The [Repeat] block saves time and effort while creating projects where blocks repeat. Rather than taking the time to drag in additional blocks or duplicate existing blocks in the workspace, the [Repeat] block can be used to save space and time.
-
Begin by modifying the previous project or create a new project to match this base project. You may use traditional blocks, Switch blocks, and a combination of both to create your project.
-
The repeated blocks in this project are the [Drive for] and the [Turn for] blocks. Remove the bottom six blocks in the project so only one [Drive for] and one [Turn for] block is left beneath the [move robot pen] block.
-
Drag in a [Repeat] block. Notice that the [Repeat] block will create a gray shadow around the blocks that will be inside when placing the block in the workspace.
-
Set the parameter of the [Repeat] block to “4” so a VR Robot draws all four sides of a square.
Using Switch Blocks
The [Repeat] block can accept integers. Decimals will not work as a parameter for a [Repeat] block.
This is the Switch [Repeat] block. In this example, the [Repeat] block is set to repeat behaviors 10 times. The command, for repeat_count in range(10), is the code that starts a loop that repeats 10 times.
The image below shows the Switch [Drive for] and [Turn for] commands that are nested inside of the Switch [Repeat] C-block. The Switch [Repeat] block's parameter is set to "4," meaning that the [Drive for] and [Turn for] commands nested within the C-block will be repeated four times.
The image below shows the same stack of Switch blocks converted into one Switch block.
for repeat_count in range(4): is the Python command that creates the repeat loop that tells the robot to repeat the indented lines of code beneath this command 4 times.
The next lines of code are indented underneath the Repeat command because these are the behaviors that will be repeated. The indentation is set to a default of 4 spaces. When typing in Python commands, be sure to always use the same number of spaces when indenting commands, as projects will not run properly if indentations are inconsistent.
The command, drivetrain.drive_for(FORWARD, 600, MM) tells the robot to drive forward 600 mm and the command, drivetrain.turn_for(RIGHT, 90, DEGREES) tells the robot to turn right for 90 degrees. Because these commands are indented under the Repeat command, these behaviors are part of the repeat loop and will be repeated four times.
-
Select the “Open Playground” button to open the Art Canvas Playground if it is not already open.
-
Select the “Start” button to test the project.
-
Watch the VR Robot drive forward and turn right four times on the Art Canvas Playground to draw a square with the Pen.
Select the Next button to continue with the rest of this Lesson.