Skip to main content

Lesson 4: Project Flow with Nested Loops

Collect the next two disks

  • Now that the VR Robot is facing the red disks, the VR Robot will need to repeat the same behaviors that were used to collect the blue disk, in order to pick up and move the red disk. The behaviors of driving forward to pick up the disk, turn around, and drive back to the same colored goal to drop off the disk.
    move red
  • Once the first red disk is moved, the VR Robot will then need to drive to the green goal and perform the same actions that were used to move the blue and red disk, to move the first green disk.
    pick up one disk
  • In order to repeat behaviors multiple times, a for loop can be used.
    for loop syntax
  • Add a for loop to the top of the project, and set the range function parameter to '3'. image of the for loop added to the top of the project
  • To copy the code into the for loop, careful attention will need to paid to the indentation. First, add space before the wait command.  image of space between the for loop command and the wait command at the top of the project
  • Next, select the commands to put into the for loop, and copy and paste them above the wait command. Notice that the indentation will be incorrect after simply copying and pasting. The gray indicator line of the for loop is broken, and disconnected from the wait command. This is a visual cue that your project will not execute as intended.image of the commands inside the for loop with incorrect indentation
  • Select the commands inside the for loop, and use 'Tab' to correctly indent the commands within the loop. When correctly indented, the gray indicator line should connect the for loop with the wait  command to show the bounds of the outer loop. When complete, your project should look like this: 
    image of the complete project correctly indented within the for loop
  • Watch the video below to see how to move the commands and adjust indentation as described above.
Video file
  • Open the Disk Mover Playground if it is not already open, and run the project.
  • When this project is run, the VR Robot picks up and drops one disk of each color into its corresponding colored goal.
    Lesson 4 Example

For Your Information

Note that the VR Robot continued to drive into the wall. This behavior is expected since the project instructed the VR Robot to turn right and drive toward the next goal, three times. The first time is driving from blue to red. The second is from red to green, and the third is from green into the wall, since there is not another colored goal. Stop the project once the VR Robot hits the wall. The project could be improved so that the VR Robot stops after it picks up and moves the green disk, but this would increase the overall complexity of the project.

program

 

 

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf