Lesson 2: If Else Statements
In this Lesson, you will create a project where the VR Robot knocks over at least two castles on the Dynamic Castle Crasher Playground. An algorithm will be needed to solve the challenge using loops and sensor feedback because the Playground layout changes with each reset.
Examples of Dynamic Castle Crasher Playground Layouts:
Learning Outcomes
- Identify that an if else statement runs the commands inside either the 'if' or the 'else' branch based on the Boolean value reported.
- Identify that an if else statement is normally used with a for loop or an infinite while loop in order to check the condition more than once.
- Identify and describe why an if else statement is used with loops.
- Describe what would cause an 'if' branch or an 'else' branch to be run in a project with an if else statement.
Why Use an If Else Statement?
The Dynamic Castle Crasher challenge is different from the Castle Crasher challenge in Unit 2. The Unit 2 challenge used simple sequencing and Drivetrain commands to knock over the castles on a static playground.
def main():
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 1550, MM)
drivetrain.turn_for(RIGHT, 180, DEGREES)
drivetrain.drive_for(FORWARD, 700, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
However, because of the changing layouts of the Dynamic Castle Crasher Playground, Drivetrain commands alone will not be sufficient to have the VR Robot knock down two or more castles. A set of Drivetrain commands may work in one Playground, but not in another. An algorithm that uses sensors and selection in iterations will be needed. The if else statement will be used to create an algorithm by adding selection with conditional statements to this project.
The If Else Statement
The if else statement is a statement that accepts Boolean conditions. The if else statement is a conditional statement that controls the flow of a project. The if else statement will execute a particular sequence of commands if the condition is met, else (when the condition is not met) another set of commands is executed – thereby ‘branching’ the project flow. Only one branch in the if else will be executed at a time.
if condition:
pass
else:
pass
- If the condition is reported as True, the commands inside of the 'if' branch are run.
- If the condition is reported as False, the commands inside of the 'else' branch are run.
- Once all commands inside one of the branches are run, the project will move on to the next command outside of the if else statement.
You likely use if else logic often in your everyday actions. For instance, when deciding what to you need to take when you leave the house based on the weather outside, you can use if else logic. If it is raining, take an umbrella; else take sunglasses.
The 'If' branch -
- In this scenario, the condition of 'raining' is met, so the 'if' branch would be executed, and you would take an umbrella.
The 'Else' branch -
- In this scenario, the condition of 'raining' is not met, so the 'else' branch would be executed, and you would take sunglasses.
The If Else Statement and the Dynamic Castle Crasher Problem
There are two main steps for the VR Robot to successfully knock over castles on the Dynamic Castle Crasher Playground. Essentially, the VR Robot must be able to locate and detect castles on the playground, and then drive to knock them over. In terms of an if else statement, this could be framed as:
- If the VR Robot detects a castle, drive forward towards it to knock it over.
- Else, (when the VR Robot does not detect a castle), turn and check for a castle in another location on the Playground.
The Distance Sensor on the VR Robot can be used to detect castles on this Playground. Unlike every other Playground, the Dynamic Castle Crasher Playground does not have walls. Because the castles are the only objects on the Dynamic Castle Crasher Playground, when the Distance Sensor detects an object, that object will be a castle. The VR Robot will need to check the condition of the Distance Sensor repeatedly, so that once these two basic steps are complete, the VR Robot can go back and use the Distance Sensor to find the next castle to knock over.
Planning a Project with the If Else Statement
- Start a new project and select the Dynamic Castle Crasher Playground when prompted.
- Name the project Unit9Lesson2.
-
Add comments into the workspace that outline the intention for the project, based on the steps identified above. The first comment explains the condition of the if else statement, and the next two explain the actions of each branch of the if else statement. Remember that Python comments begin with a # (pound) symbol. Your project should look like this:
def main(): # Does the Distance Sensor detect a castle? # Crash castle detected by a Distance Sensor # Turn to find a castle using the Distance Sensor
-
An if else statement can be used to trigger each of the actions outlined above. Each action depends on if the Boolean condition in the if else statement reports as True or False. Add an if else statement after the comments. Your project should now look like this:
def main(): # Does the Distance Sensor detect a castle? # Crash castle detected by a Distance Sensor # Turn to find a castle using the Distance Sensor if condition: pass else: pass
-
Each branch of the if else statement can be defined using the comments. If the Distance Sensor detects a castle, the VR Robot should drive to knock over the castle; else it should turn to detect a castle. Move the if and else branches of the project to align with the comments describing the associated actions. Your project should now look like this:
def main(): # Does the Distance Sensor detect a castle? if condition: # Crash castle detected by a Distance Sensor pass else: # Turn to find a castle using the Distance Sensor pass
Select the Next button to continue with the rest of this Lesson.