Skip to main content

Lesson 3: Code Colorization in VEXcode VR Python

Three VEXcode VR Python commands to illustrate the concept of code colorization.

You now know the basics of getting started with VEXcode VR Python. You probably noticed that the lines of code in VEXcode VR Python become colorized when they are added to the Workspace. This colorization is a helpful visual tool that you can use while programming.

Knowing how VEXcode VR Python code is colorized can help you to quickly identify errors in project syntax or spelling mistakes, that might prevent your project from running as intended. Understanding code colors and their meaning will be valuable as you begin creating VEXcode VR Python projects.

Learning Outcomes

  • Identify and describe the colorization of code in VEXcode VR Python.
  • Identify how to indent commands in the Workspace, and the importance of doing so in a project.

Colorization of Code in VEXcode VR Python

When creating Text projects, the syntax, spacing, indenting, and spelling in your code in very important to ensure that your project runs as intended. The color coding present in the Workspace in VEXcode VR Python is an extra visual cue that you have entered your code into your project correctly.

The colorization of code follows the following conventions, as shown in the table below, or in this VEX Library article:

Color Type VEXcode VR Python Example
Blue

Classes

the individual device the command pertains to 

(i.e. drivetrain, pen, brain)

An example of a drive command with the blue drivetrain portion highlighted in a red box to illustrate the class.
Brown

Commands 

the behavior within the command 

(i.e. drive, turn)

A drive command with the 'drive' portion highlighted in a red box to indicate the 'command'.
Orange

Parameters 

information about how the behavior executes

(i.e. direction, distance)

A drive command with the orang parameter of forward highlighted to indicate that part of the command.
Royal Blue

Structures 

control the flow of the project

(i.e. conditional, loops)

A command reads if left bumper pressed, an the royal blue if is highlighted in a red box indicating the structure portion of the command.
Green

Values 

numerical parameter assigned to the behavior

(i.e. number of degrees of a turn)

A turn for command with the green 90 parameter highlighted in a red box to indicate the value portion of the command.
Black

User Created 

user created variables and commands

Two commands with user created variables are shown, the first reads for value in range 10, and the value portion is highlighted in a red box. The second reads if left bumper pressed () : with the parentheses and colon highlighted, illustrating the user created portions of these commands.

 

Comments in a project are also colored green, but are preceded by a pound (#) symbol. An example of a green Comment in a project that reads # Comments in a project.

When typing commands into the Workspace, if there is an unrecognizable component, it will remain black (like the user-created category). This can be a useful indicator as you are working, to prevent frustration or extra troubleshooting later on.

Use the cursor and keyboard to correct errors. As the components are recognized, they will be colorized correctly. In the video clip below, a turn_for command is typed into the project with two incorrectly spelled components that show as black. As each error is corrected, the color of that portion of the command changes.

Video file

The Importance of Indentation

In VEXcode VR Python, indentation is VERY important. The way that commands are aligned within a project contributes to the flow of a project. Improper indentation can result in a project not executing as intended, or not at all. Commands that are aligned with one another will run as a sequence. Commands that are indented within other commands (like the commands inside of a loop) will run within that structure. You will learn more about indentation throughout this course. An example of a correctly indented project with red arrows indicating the different levels of indentation. The for loop is indented within the def main function, and the drivetrain commands within the for loop are indented further.

Like the colorization of code, there are visual cues for proper indentation in VEXcode VR Python. The gray lines on the left side of the project, show how the project will flow. There should be a contiguous line from the main function through the entire project, as indicated by the arrow in this image. An example of a correctly indented project where the main function indicator line to the left, beneath the def main command is solid alongside the commands in the project.

Any subsequent loops or control structures will also have their own indicator lines, showing the start and end of the structure. In this example, the indicator line of the for loop is highlighted by the arrow. A correctly indented project in which the indicator line of the for loop is shown to extend from the for loop solidly to the left of the drivetrain commands repeated within.

Indenting Commands 

As your VEXcode VR Python project grow more complex, indentation will play an important role in how your projects are constructed. 

  • When adding commands to a project, use the 'Tab' button on the keyboard to indent lines of the project appropriately. Place your cursor at the start of the command, then press 'Tab' until the command is indented to the needed location.
    • To indent multiple commands the same way, select the commands you wish to indent, then use the 'Tab' button on the keyboard to move all of them at one time.
  • In the video clip below, there are three commands that should be indented within the for loop. The first is individually tabbed over when the cursor is placed at the start of the line. Then the remaining two are highlighted, and tabbed over to align, so that the project will execute correctly.
    • Notice that the indicator line for the for loop appears when the commands are correctly indented.   
Video file