Switch#

Introduction#

Switch blocks enable students to write Python code directly within a block-based environment. This approach helps bridge the gap between block-based and text-based programming. By allowing users to see how block commands convert into Python, Switch blocks make it easier to learn text-based coding techniques within a familiar interface. Using Switch blocks can simplify advanced logic, calculations, and function calls while still using normal Blocks.

Stack#

The Switch Stack block is used to execute Switch Python commands typed within the open space of the block.

They are shaped to attach above or below other action blocks.

a VEXcode Switch Blocks Example showing a Switch Stack Block#
[] :: custom-switch

Switch Stack blocks can be used for single lines of Switch Python commands or multiple lines.

In order for a Switch Stack block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode Switch Blocks Example showing a Switch Stack Block with switch python commands for driving forward for 400mm#
当开始 :: hat events
[Move the robot forward.]
[drivetrain.drive(FORWARD)] :: custom-switch

C Block#

The Switch C Block block is used to loop the blocks within until the condition in the open space of the block is met.

They are shaped to attach stack or other C blocks above, below, or inside them.

a VEXcode Switch Blocks Example showing a Switch C Block#
if [] :: custom-switch
结束

Some possible Switch Python commands to use with the Switch C block include:

  • while

  • if

  • for

In order for a Switch C block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode Switch Blocks Example with text inside of the switch c block and a print block saying bumper was pressed#
当开始 :: hat events
[Turn while the bumper is held down.]
if [while ledbumper_1.is_pressed():] :: custom-switch
[右 v] 转
结束
驱动停止
结束

C Expandable#

The Switch C Expandable block is used to loop the blocks within a specific branch when the condition in the open space of the block is met. Additional branches can be added by expanding the block.

They are shaped to attach stack and other C blocks above, below, or inside them.

a VEXcode Switch Blocks Example showing a Switch C Expandable block#
if [] :: custom-switch-expand
结束

Some possible Switch Python commands to use with the Switch C Expandable block include:

  • while

  • if

  • for

In order for a Switch C Expandable block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode Switch Blocks Example showing a Switch C Expandable with if else python code#
当开始 :: hat events
[Turn right if a button is held down, otherwise turn left.]
永久循环
if [if ledbumper_1.is_pressed():] :: custom-switch-expand
[右 v] 转
else [else:                                          ]
[左 v] 转
结束
结束

Boolean#

The Switch Boolean block is used to return a condition as either True or False. These conditions are defined with Switch Python functions.

They are shaped to fit inside any blocks with hexagonal (six-sided) inputs.

a VEXcode Switch boolean Block#
<[] :: custom-switch>

Switch Boolean blocks can be used for single lines of Switch Python functions that directly will return a true or false condition.

Operators can be used to evaluate multiple Switch Python functions in a single Switch Boolean block. This includes:

  • and

  • or

  • not

a VEXcode switch Block example with a boolean block that has a line of switch python functions#
<[eye.is_object_detected() and ledbumper_1.is_pressed()] :: custom-switch>

A Switch Boolean block can also combine Switch Python functions with comparison operators to evaluate how a value returned compares to the given number using the following operators:

  • Greater Than: >

  • Less Than: <

  • Equal to: =

  • Additional operators that can be used include <=, !=, and >=

a VEXcode switch block example using a boolean Block with a comparison operator#
<[drivetrain.get_heading()> 100] :: custom-switch>

In order for a Switch Boolean block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode switch block expample using a boolean Block with comparison operator#
当开始 :: hat events
[Spin in a circle when a button is pressed.]
永久循环
如果 <[ledbumper_1.is_pressed()] :: custom-switch> 那么
[右 v] 转 (360) 度 ▶
结束
结束

Reporter#

The Switch Reporter block is used to return a numeric value. These conditions are defined with Switch Python functions.

They are shaped to fit inside any blocks with circular inputs.

a VEXcode Switch Blocks Example showing a Switch Reporter Block#
([] :: custom-switch)

Switch Reporter blocks can be used for single lines of Switch Python functions that directly will return a numeric value.

Math functions can be used to calculate values within a Switch Reporter block. This includes, but is not limited to:

  • Addition: +

  • Subtraction: -

  • Division: /

  • Multiplication: *

a VEXcode Switch Blocks Example showing a Switch Reporter Block with python code that has an addition operator#
([drivetrain.get_heading() + 45] :: custom-switch)

In order for a Switch Reporter block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode Switch Blocks Example showing a Switch Reporter Block within a print block#
当开始 :: hat events
[Turn and display the current heading.]
[右 v] 转
等待 (2) 秒
驱动停止
打印 ([drivetrain.get_heading()] ::custom-switch) ▶

Hat#

The Switch Hat block is used to run the attached stack of blocks when the function is called.

They are shaped to attach above other action blocks.

a VEXcode Switch Blocks Example showing a Switch hat block#
[] :: hat custom-switch

The attached stack of blocks are a function. The Switch Hat block is used to define the name of the function and any optional parameters.

Parameters should be enclosed in parentheses and followed by a colon as shown below. If a function has no parameters, use closed parentheses: ().

a VEXcode Switch Blocks Example showing a Switch hat block with python function that has a parameter#
[def function_name(parameter):] :: hat custom-switch

In order for a Switch Hat block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.

a VEXcode Switch Blocks Example showing a Switch hat block and a drive forward block#
[drive_400()] :: hat custom-switch
[Create a function to drive a robot forward.]
驱动 [向前 v] (400) [毫米 v] ▶
当开始 :: hat events
[drive_400()] :: custom-switch