How code embedding in operators works Pro Preview

Python requires the code to be ordered in a strictly specific manner in order to work properly and as you can imagine, this becomes complicated when we start nesting operators inside one another.

Here, we will show sine things you should look out for when writing code and some good practices for evading some common mistakes.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #1633
  • 12 Jun 2020

Indented code indicates to Python, that it is nested inside an operator. 

Here's an example of some nested code inside a "while" loop operator:

while (condition):
    (nested code inside a "while" loop)
    (nested code inside a "while" loop)
    (nested code inside a "while" loop)
    ...

There is no requirement for how much indentation there should be for the indented code itself, but it is important for each line to be indented with the same amount of space in order for it to work.

while (condition): 
    (nested code inside a "while" loop)
   (nested code inside a "while" loop)
     (nested code inside a "while" loop)
     ...

To avoid this mistake, many programmers use the "Tab" key, which is used to align the code in specific lines placed at 4-character distances.

When nesting operators inside each other, the code in each of them must be indented one level to the right:

while (condition):
    (nested code inside a "while" loop)
    (nested code inside a "while" loop)
    if (condition):
        (nested code in an "if" operator)
        (nested code in an "if" operator)
  ...

The presence of a nested operator does not mark the end of the code in which it is nested. Example:

while (condition): 
    (nested code inside a "while" loop) 
    (nested code inside a "while" loop) 
    if (condition):
        (nested code in an "if" operator)
        (nested code in an "if" operator)
    (nested code inside a "while" loop)
    (nested code inside a "while" loop)
...

Note that the last two lines of the nested code inside of the "while" loop are indented by just a single Tab, making it part of the "while" loop code, but not the "if" operator. These two lines will be executed on every iteration of the loop regardless of the condition of the "if" operator.

Nesting operators and code indentation is not limited to nesting just a single operator and for us to be able to keep track of this, VS-Code places vertical lines in our code that shows the beginning and the end of every nested code:

while (condition):
|   (nested code inside a "while" loop)
|   (nested code inside a "while" loop)
|   
|   if (condition):
|   |   (nested code in an "if" operator) 
|   |   (nested code in an "if" operator)
|   |
|   |   while (condition):
|   |   |   (nested code inside a "while" loop)
|   |   |   (nested code inside a "while" loop)
|   |   
|   |   (nested code in an "if" operator)
|
|   else:
|   |   (nested code in an "else" operator)
|   |   (nested code in an "else" operator)
|
|   if (condition):
|   |   (nested code in an "if" operator)
|   |   (nested code in an "if" operator)
|
|   (nested code inside a "while" loop)
|   (nested code inside a "while" loop)

(code outside of an operator)
(code outside of an operator)
...

As you can see, having empty lines at the beginning and end of each operator makes our code much more readable.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Python with LEGO Mindstorms EV3 - Level 2
  • 39
  • 19:58
  • 93
Image for Lesson 3 - Crane movement control panel
  • 3
  • 4
  • 6
  • 3d_rotation 2