Skip to main content

Lesson 4: Using Infinite While Loops

Solving the Disk Maze Problem

Building the project

  • Load the Unit7Lesson3 project from the previous Lesson, or recreate the project shown here to begin.
def main():
	# If front eye detects green then turn right
    if front_eye.detect(GREEN):
        drivetrain.turn_for(RIGHT, 90, DEGREES)

    # If front eye detects blue then turn left
    if front_eye.detect(BLUE):
        drivetrain.turn_for(LEFT, 90, DEGREES)

    # If front eye detects red then stop
    if front_eye.detect(RED): 
        drivetrain.stop()

    # If front eye detects none then drive forward
    if front_eye.detect(NONE): 
        drivetrain.drive(FORWARD)
  • Rename the project Unit7Lesson4.VEXcode VR Toolbar with the project name box called out. The project is named Unit 7 Lesson 4.

     

  • Drag in or type an infinite while loop to the top of the project, and set the condition to True.
def main():
    while True:
    	wait(5, MSEC)
  • Move the wait command to make space for the commands that will go inside the infinite while loop.
def main():
    while True:
    
    
    	wait(5, MSEC)
  • Inside the infinite while loop, copy and paste the previous if statements inside the loop. Do not delete the wait command at the bottom of the while loop. Your project may look like this. Notice that copying and pasting commands in this way does not result in correct indentation.
def main():
    while True:
        
    # If front eye detects green then turn right
    if front_eye.detect(GREEN):
        drivetrain.turn_for(RIGHT, 90, DEGREES)

    # If front eye detects blue then turn left
    if front_eye.detect(BLUE):
        drivetrain.turn_for(LEFT, 90, DEGREES)

    # If front eye detects red then stop
    if front_eye.detect(RED): 
        drivetrain.stop()

    # If front eye detects none then drive forward
    if front_eye.detect(NONE): 
        drivetrain.drive(FORWARD)

    wait(5, MSEC)
  • Remember, indentation is VERY important in VEXcode VR Python. In order for the project flow to execute as intended, the if statements need to be indented within the infinite while loop. Select the if statements and press 'Tab' to indent them within the infinite while loop. When properly indented, your project should look like this: 
def main():
    while True:
        
        # If front eye detects green then turn right
        if front_eye.detect(GREEN):
            drivetrain.turn_for(RIGHT, 90, DEGREES)

        # If front eye detects blue then turn left
        if front_eye.detect(BLUE):
            drivetrain.turn_for(LEFT, 90, DEGREES)

        # If front eye detects red then stop
        if front_eye.detect(RED): 
            drivetrain.stop()

        # If front eye detects none then drive forward
        if front_eye.detect(NONE): 
            drivetrain.drive(FORWARD)

        wait(5, MSEC)
  • With the infinite while loop added to the project, all of the if statements will repeatedly have their conditions checked. Open the Disk Maze Playground and run the project.
  • When this project is run, the VR Robot will navigate the Disk Maze Playground from start to finish. Once the VR Robot detects red, it will stop driving.

    VR Robot on the Disk Maze Playground with the VR Robot stopped in front of the red disk near the bottom right corner of the playground.

In Summary

  • In the project from the previous Lesson, the conditions within the if statements were only checked once. Since the conditions within the if statements were only checked once, the VR Robot drove forward forever because the condition of 'no color' being detected was reported as True at the beginning of the project.
  • Now that the infinite while loop has been added, each condition of the if statements will be checked repeatedly. The condition of the while loop is always True, and therefore the code inside the while loop will always execute. This will repeatedly check the conditions of the if statements while the project is running. 
  • During the flow of the project, if the condition of the if statements is True, the commands inside of the if statement are executed. If the condition of the if statement is False, the commands inside of the if statement are not executed, and the flow of the project will continue to the next command. This enables the VR Robot to perform discrete behaviors such as turning or stopping once it detects a certain color.
  • If statements are used with infinite while loops to ensure that conditions are constantly checked.

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf