Lesson 3: Using If Statements
Navigating the Disk Maze Using If Statements
In the Disk Maze Challenge, the VR Robot is checking the condition of the Front Eye Sensor to see what color is being reported. Each of the colors aligns to a different behavior the VR Robot should perform.
For Your Information
The if statement accepts only Boolean conditions. Using the patterns observed while solving the Disk Maze in Lesson 2, a project can be created where the VR Robot makes a decision if the condition reports as True or False.
if condition:
pass
The if statement will run one time in a project. In this example, the Front Eye Sensor will report True if a green object is detected, and execute the command inside the if statement - turn right for 90 degrees. If a green object is not detected, the project will move to the next command.
def main():
if front_eye.detect(GREEN):
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.stop()
- Start a new project and select the Disk Maze Playground when prompted.
-
Name it Unit7Lesson3.
- Add comments to describe the intention for the behaviors of the VR Robot during each following section of the project.
def main():
# If front eye detects green then turn right
# If front eye detects blue then turn left
- Drag in or type the if statement beneath the first comment.
def main():
# If front eye detects green then turn right
if condition:
pass
# If front eye detects blue then turn left
- Set the condition of the if statement to the front_eye.detect command, and set the parameter to 'GREEN.'
def main():
# If front eye detects green then turn right
if front_eye.detect(GREEN):
pass
# If front eye detects blue then turn left
- Drag in or type a turn_for command inside the if statement, to replace the 'pass'. Set the parameters to turn right 90 degrees. Now, if the Front Eye Sensor detects 'GREEN', the VR Robot will turn right 90 degrees.
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
- We will follow these same steps to add additional if statements for all three colors in the Disk Maze (green, blue, and red). Each color is associated with a different behavior. The commands inside the if statements will need to match that intended behavior. Add the following commands to the project beneath the second comment, so that if the Front Eye Sensor detects the color 'BLUE,' the VR Robot will turn left 90 degrees.
# If front eye detects blue then turn left
if front_eye.detect(BLUE):
drivetrain.turn_for(LEFT, 90, DEGREES)
-
When the VR Robot reaches the end of the maze, the Front Eye Sensor will detect the color ‘RED.’
- To stop the VR Robot, another if statement needs to be added with instructions for what to do if the Front Eye Sensor detects ‘RED.’ Add the following comment and commands to the project so that the VR Robot stops driving if it detects 'RED.'
# If front eye detects red then stop
if front_eye.detect(RED):
drivetrain.stop()
If None of the Colors are Detected
The VR Robot can also make a decision if it does not see a color by using the ‘none’ value. For instance, at the starting point of the Disk Maze, the Front Eye Sensor does not detect any colors.
Because the Front Eye Sensor does not detect a color at the beginning of the Disk Maze, the VR Robot will need to drive forward until it detects the green disk. Another if statement can be added to instruct the VR Robot for what to do when no color is detected.
- Add the following comment and commands to the project, so that if the Front Eye Sensor does not detect any color, the VR Robot will drive forward.
# If front eye detects none then drive forward
if front_eye.detect(NONE):
drivetrain.drive(FORWARD)
- The VR Robot now has instructions for each color detected by the Front Eye Sensor.
- If Front Eye Sensor detects 'GREEN' – Turn right 90 degrees
- If Front Eye Sensor detects ‘BLUE' – Turn left 90 degrees
- If Front Eye Sensor detects ‘RED’ – Stop driving
- If Front Eye Sensor detects ‘NONE’ – Drive forward
- Open the Playground Window if it is not already open. Be sure the Disk Maze Playground opens, and run the project. The entire project should look like this:
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)
- Does the VR Robot behave as you intended it to? Why or why not? On the next page of the Lesson, we will learn more about this project.
Select the Next button to continue with the rest of this Lesson.