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 EXP 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, orforstatements.C Expandable — Provides a space to type Python control code with multiple branches, such as
if,elif, orelse.Boolean — Provides a space to type Python code that returns
TrueorFalse.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.
[] :: custom-switch
A Stack block can contain one line or multiple lines of Python code.
when started :: hat events
[Drive forward, then stop.]
[drivetrain.drive(FORWARD)] :: custom-switch
wait [2] seconds
stop driving
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.
if [] :: custom-switch
end
Python control code that can be used in a C Block includes:
when started :: hat events
[Drive forward when the screen is pressed.]
forever
if [if brain.screen.pressing():] :: custom-switch
drive [forward v] for [200] [mm v] ▶
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.
if [] :: custom-switch-expand
end
Python control code that can be used in a C Expandable block includes:
when started
[Turn right when the screen is pressed, otherwise turn left.]
forever
if [if brain.screen.pressing():] :: custom-switch-expand
turn [right v]
else [else:] :: custom-switch-expand
turn [left 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.
<[] :: custom-switch>
A Boolean block can contain a Python condition.
<[bumper_a.is_pressed()] :: custom-switch>
A Boolean block can also use logical operators:
<[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>=
<[drivetrain.get_heading() > 100] :: custom-switch>
when started :: hat events
[Drive forward whenever the screen is pressed.]
forever
if <[brain.screen.pressing()] :: custom-switch> then
drive [forward v] for [200] [mm v] ▶
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.
([] :: custom-switch)
A Reporter block can contain Python code that returns a value.
([drivetrain.rotation()] :: custom-switch)
Math operators can be used in a Reporter block:
([drivetrain.rotation() + 45] :: custom-switch)
when started :: hat events
[Display the rotation while turning.]
turn [right v] for [450] degrees ◀ and don't wait
forever
clear all rows
print ([drivetrain.rotation()] :: custom-switch) ▶
wait [0.1] 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 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.
[def function_name(parameter):] :: hat custom-switch
[def drive_400():] :: hat custom-switch
[Create a function to drive the robot forward.]
drive [forward v] for (400) [mm v] ▶
when started :: hat events
[drive_400()] :: custom-switch