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 CTE 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 move the 6-Axis Arm to a position.#
  when started :: hat events
  [Move the 6-Axis Arm to a standard position at the start of the project.]
  [arm.move_to(120, 120, 75, False)] :: 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:

When started, uses a C Block to check if the 6-Axis Arm can reach a position.#
  when started :: hat events
  [Check if the 6-Axis Arm can move to a position.]
  if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch
    print [The 6-Axis Arm can't move to this position.] on console ◀ and set cursor to next row
  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:

When started, uses a C Expandable block to check if the 6-Axis Arm can reach a position.#
  when started
  [Check if the 6-Axis Arm can or can't move to a position.]
  if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch-expand
    print [The 6-Axis Arm can't move to this position.] on console ◀ and set cursor to next row
  else [else:] :: custom-switch-expand
    print [The 6-Axis Arm can move to this position.] on console ◀ and set cursor to next row
  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_a.is_pressed()] :: custom-switch>

A Boolean block can also use logical operators:

Boolean block with a Python logical operator#
<[bumper_a.is_pressed() or bumper_b.is_pressed()] :: 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.get_heading() > 100] :: custom-switch>
When started, uses a Boolean block to check if the 6-Axis Arm can reach a position.#
when started
[Check if the 6-Axis Arm can move to a position.]
if <not <[arm.can_arm_reach_to(0, 0, 0)] :: custom-switch>> then
  print [The 6-Axis Arm can't move to this position.] on console ◀ and set cursor to next row
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#
([arm.get_y()] :: custom-switch)

Math operators can be used in a Reporter block:

  • Addition: +

  • Subtraction: -

  • Division: /

  • Multiplication: *

Reporter block with Python addition#
([arm.get_y() + 45] :: custom-switch)
When started, prints the current y-position while the 6-Axis Arm moves.#
when started
[Display the current y-position while the 6-Axis Arm moves.]
move [arm v] to position x:(-100) y:(200) z:(100) [mm v] ◀ and don't wait
repeat until <[arm v] is done moving?>
  print ([arm.get_y()] :: custom-switch) on console ◀ and set cursor to next row
  wait [0.25] seconds
end

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 increment_y_100():] :: hat custom-switch
[Create a function to increment the 6-Axis Arm.]
increment [arm v] position by x:[0] y:[100] z:[0] [mm v] ▶

when started :: hat events
[increment_y_100()] :: custom-switch