Switch#

Introduction#

Switch blocks help students transition from block-based coding to text-based coding. With Switch blocks, students can type Python code directly inside a blocks project — writing commands, conditions, values, or function definitions — while still building the rest of the project with blocks. Individual blocks can also be converted into their equivalent Python commands.

To learn how to type Python commands, methods, and operators, see the VEX VR Python API.

Because Switch blocks contain Python code, spelling, punctuation, and indentation must be correct for the project to run as expected.

Below is a list of all Switch blocks:

Switch blocks — Add Python code to a Blocks project.

  • Stack — Provides a space to type Python commands that run in sequence with other stack blocks.

  • C Block — Provides a space to type Python control code, such as if, while, or for statements.

  • C Expandable — Provides a space to type Python control code with multiple branches, such as if, elif, or else.

  • Boolean — Provides a space to type Python code that returns True or False.

  • Reporter — Provides a space to type Python code that returns a value.

  • Hat — Provides a space to define a Python function.

Stack#

The Stack block provides a space to type Python commands that run in sequence with other stack blocks.

Stack block#
 [] :: custom-switch

A Stack block can contain one line or multiple lines of Python code.

When started, uses a Stack block to drive forward.#
when started
[Drive forward 400 mm.]
[drivetrain.drive_for(FORWARD, 400, MM)] :: custom-switch

C Block#

The C Block provides a space to type Python control code, such as if, while, or for statements. Blocks placed inside the C Block run as part of that Python control structure.

C Block#
if [] :: custom-switch
end

Python control code that can be used in a C Block includes:

  • if

  • while

  • for

When started, uses a C Block to stop when the bumper is pressed.#
when started
[Continuously drive until the bumper is pressed.]
drive [forward v]
forever
  if [if bumper.pressed():] :: custom-switch
    print [Bumper was pressed!] ▶
    stop driving
  end
end

C Expandable#

The C Expandable block provides a space to type Python control code with multiple branches, such as if, elif, or else. Additional branches can be added by selecting the plus arrow.

C Expandable block#
if [] :: custom-switch-expand
end

Python control code that can be used in a C Expandable block includes:

  • if

  • elif

  • else

  • while

  • for

When started, uses a C Expandable block to stop when the bumper is pressed and drive otherwise.#
when started
[Continuously move forward until the bumper is pressed.]
forever
  if [if bumper.pressed():] :: custom-switch-expand
    print [Bumper was pressed!] ▶
    stop driving
  else [else:] :: custom-switch-expand
    drive [forward v]
  end
end

Boolean#

The Boolean block provides a space to type Python code that returns True or False. It can be placed inside blocks with hexagonal inputs.

Boolean block#
<[] :: custom-switch>

A Boolean block can contain a Python condition.

Boolean block with a Python condition#
<[bumper.pressed()] :: custom-switch>

A Boolean block can also use logical operators:

  • and

  • or

  • not

Boolean block with a Python logical operator#
<[bumper.pressed() or distance.found_object()] :: custom-switch>

A Boolean block can use comparison operators to compare values:

  • Greater than: >

  • Less than: <

  • Equal to: ==

  • Other comparison operators include <=, !=, and >=

Boolean block with a Python comparison operator#
<[drivetrain.heading() > 100] :: custom-switch>
When started, uses a Boolean block to stop when the Distance Sensor finds an object.#
when started
[Turn right until the Distance Sensor finds an object.]
turn [right v]
forever
  if <[distance.found_object()] :: custom-switch> then
    print [Object found!] ▶
    stop driving
  end
end

Reporter#

The Reporter block provides a space to type Python code that returns a value. It can be placed inside blocks with circular inputs.

Reporter block#
([] :: custom-switch)

A Reporter block can contain Python code that returns a value.

Reporter block with Python code that returns a value#
([drivetrain.heading()] :: custom-switch)

Math operators can be used in a Reporter block:

  • Addition: +

  • Subtraction: -

  • Division: /

  • Multiplication: *

Reporter block with Python addition#
([drivetrain.heading() + 45] :: custom-switch)
When started, prints the robot’s heading plus 45 degrees.#
when started
[Print the robot's heading plus 45 degrees.]
print ([drivetrain.heading() + 45] :: custom-switch) ▶

Hat#

The Hat block provides a space to define a Python function. Blocks attached under the Hat become the body of that function.

Hat block#
[] :: hat custom-switch

Type a Python function definition in the Hat block. Parameters go inside the parentheses. If the function does not use parameters, use empty parentheses.

Hat block with a Python function definition#
[def function_name(parameter):] :: hat custom-switch
When started, uses a Hat block to define and call a function.#
[def drive_400():] :: hat custom-switch
[Define a custom function using a Hat block.]
drive [forward v] for (400) [mm v] ▶

when started
[drive_400()] :: custom-switch