Skip to main content

Lesson 2: Flow of a Project with a For Loop

When a project uses a for loop, it still follows the same logic while executing the commands. For instance, these two code samples will make the VR Robot move in the same way. However, the project with the for loop, repeats the two Drivetrain commands four times in order to do so.

Two code snippets to drive in a square shown side by side to illustrate the difference between using the for loop and not. The one on the left has no for loop, and has the drive for and turn for commands repeated four times each. The one on the right has a for loop, and red arrows in a circular pattern with the words 4 times inside them, indicating that the for loop causes the drive for and turn for commands to repeat four times.

A for loop is used to repeat a series of commands a specified number of times. The for loop repeats (also known as iterates) over the sequence of commands inside it. In a Python for loop, the variable 'value' is used to track the current iteration, and the key word 'in' uses the range function to specify the number of iterations. Breakdown of the syntax of a for loop shown with text above each word in the command to identify its function. The first word, for, has the text "identifies the loop" above it. The second word, value, has the text: "Identifies the variable (how many times has it iterated through the loop?). The third word, in, has the text "Connects the variable to the list (range). The last word is range with the parameter 10 in parenthesis. The text above it reads, "Identifies the list (How many times will it iterate through the loop?).

The range function identifies how many times the loop should iterate (or repeat). To repeat the behaviors for the desired number of times, set the parameter of the range function.

The range function essentially creates a temporary list that specifies what the loop should iterate through, in order to be completed. In the case of repeating behaviors, like drawing the four sides of a square, the range function uses a temporary list of (0, 1, 2, 3) to enable the VR Robot to execute those commands four times. The movement of the VR Robot will change depending on the parameter of the range function, as shown here. 

VEXcode project illustrating the range function. It reads def main open parenthesis close parenthesis colon next line which is indented reads pen dot move open parenthesis down close parenthesis next line for value in range open parenthesis 0 close parenthesis colon next line which is indented reads drivetrain dot drive underscore for open parenthesis forward, 600, MM close parenthesis next line drivetrain dot turn underscore for open parenthesis right, 90, degree close parenthesis next line wait open parenthesis five comma M S E C). Art Canvas Playground with the robot in the center. Drivetrain commands executed 0 times.
The same project as above, but with 1 in the parameter of the range function. Art Canvas Playground with project to draw one side of a square complete. Drivetrain commands executed 1 time.
The same project as above, but with 2 in the parameter of the range function. Art Canvas Playground showing that the robot has drawn the first two sides of the square. Drivetrain commands executed 2 times.
The same project as above, but with 3 in the parameter of the range function. Art Canvas Playground showing that the robot has drawn three sides of the square. Drivetrain commands executed 3 times.
The same project as above, but with 4 in the parameter of the range function. Art Canvas Playground showing that the robot has drawn all four sides of the square. Drivetrain commands executed 4 times, completing all 4 sides of the square.

 

The same project as above, but with 5 in the parameter of the range function.

Art Canvas Playground showing that the robot has drawn all four side of the square, and then traced over the first side again. Drivetrain commands executed 5 times, completing the square, then tracing over the first side again.

Notice that the drawn shape of the square did not change when the range function changed from four to five; but the final position of the VR Robot did. As such, using a for loop with the default range function of ten, would result in the same square shape being drawn, but the VR Robot would continue to repeat the behaviors an additional six times. 

The VR Robot will move through the commands from the for loop header to the last indented command. The for loop header indicates that the VR Robot should repeat the actions inside the loop the specified number of times before moving on to the next command outside of the for loop. The indented lines indicate what commands are inside the for loop and should be repeated.

For Your Information

In VEXcode VR, a wait command is always added with the for loop. The purpose of the wait command is to ensure that VEXcode VR can properly run the project as intended, due to the web-based nature of the VEXcode VR platform. The wait command should never be deleted when using a for loop, or your project may not run as intended.

For loop with a wait command.

The specified number of times that the for loop is set to execute, enables the project to flow through out of the for loop when it has completed the necessary iterations. The parameter of the range function in the for loop must be reached before the project will move on to the next command outside the loop. In the code sample below, once the Drivetrain commands in the for loop have been executed four times, the project will move out of the for loop, set the pen color to red, and drive in reverse. The VEXcode project from above, with 4 in the range parameter shown with red arrows around the for loop and the notation 4x showing that the project would iterate through the loop four times. 2 additional lines of code are added at the bottom. Arrows show that those 2 lines would execute once the four iterations are complete. Those lines read, pen dot set color open parenthesis red close parenthesis and drivetrain dot drive underscore for open parenthesis reverse comma 600 comma mm close parenthesis. To the right is a picture of the project after it has been run on the playground showing a black square with a red line coming out of it drawn by the VR robot.

Mini Challenge

In this project, the VR Robot should draw a blue square on the Art Canvas Playground, but there are errors in the project. Edit the project to solve the mini challenge!

Art Canvas playground with a blue square drawn by the VR Robot.

The video below shows what to do to complete the mini challenge. The robot starts in the middle of the playground and draws the four sides of a large blue square. 

Video file
  • Watch the solution video and review how the VR Robot should move in order to complete the mini challenge.
  • Modify the Unit3Lesson2 project to match this image, or copy the code below.
     
def main():
	pen.move(DOWN)
	pen.set_pen_color(RED)
	drivetrain.drive_for(FORWARD, 600, MM)
	
	for value in range(3):
		drivetrain.drive_for(FORWARD, 600, MM)
		drivetrain.turn_for(RIGHT, 90, DEGREES)
		wait (5, MSEC)
  • Start the project and identify where the VR Robot does not complete the expected behaviors.
  • Modify the project and run the project again. See if the VR Robot completes the expected behaviors.
  • If the VR Robot does not draw a blue square, repeat the previous step and try again. Continue to modify and run your project until the VR Robot draws a blue square.
  • Once the VR Robot successfully draws a blue square on the Art Canvas Playground, save the project.

Congratulations! You solved the mini challenge!

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf