The "if" logical operator allows us to predict the conditions which the robot could wind up in, but what do we do if the conditions aren't met? Luckily. we have a solution to this problem.
- 11 Jun 2020
The "else" acts as an "Otherwise" expression. This operator can be written right after the "if" operator and will only execute its code if the "if" condition is not met.
Here's how your code should look when using this command:
if condition: (code for met condition) else: (code for UNmet condition) (code that will always be executed either way)
⚠️ Notice the colon after the "else" command. It's common to forget to place it in the beginning and the students wonder why their program does not work!
The "else" operator does not require a condition, which makes it useful for planning for unforseen situations.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons