转换#

简介#

Switch 指令块帮助学生从基于指令块的编程过渡到基于文本的编程。借助 Switch 模块,学生可以直接在指令块项目中输入 Python 代码——编写命令、条件、值或函数定义——同时仍然可以使用指令块构建项目的其他部分。单个指令块也可以转换为相应的 Python 命令。

要了解如何输入 Python 命令、方法和运算符,请参阅 VEXcode AIR Python API。

由于 Switch 指令块包含 Python 代码,因此拼写、标点符号和缩进必须正确,项目才能按预期运行。

以下是所有 Switch 指令块的列表:

切换指令块 — 向 Blocks 项目添加 Python 代码。

  • 指令块 — 提供一个空间来键入与堆栈块按顺序运行的 Python 命令。

  • [* * C块* *] (#c-block) —提供用于键入Python控制代码的空格,例如 if、 while或 for 语句。

  • [* * C可扩展* *] (#c-expandable) —提供用于使用多个分支,如 if、 elif或 else键入Python控件代码的空格。

  • [* * Boolean * *] (#boolean) —提供用于键入返回 True 或 False的Python代码的空格。

  • 报告器 — 提供一个空间来键入返回值的 Python 代码。

  • 帽 — 提供一个空间来定义 Python 函数。

堆栈#

指令 块提供了一个空间,用于键入与其他指令块按顺序运行的 Python 命令。

指令块#
[] :: custom-switch

指令 块可以包含一行或多行 Python 代码。

当开始,使用指令块起飞,等待片刻,然后着陆。#
当开始
[Take off, wait 1 second, then land.]
[drone.take_off(500)] :: custom-switch
等待 [1] 秒
土地 ▶

C 型指令块#

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 型指令块#
if [] :: custom-switch
结束

可在C 指令块中使用的 Python 控制代码包括:

  • [if] (…/python/Logic/Controls.md#if)

  • while

  • for

当开始,使用 C指令块跟随控制器移动,直到按下按钮 7。#
当开始
[Move with the controller until button 7 is pressed.]
起飞至 [500] [毫米 v] ▶
if [while not controller.button7.pressing():] :: custom-switch
使用遥控器移动
结束
土地 ▶

C 型可扩展指令块#

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 可扩展块#
if [] :: custom-switch-expand
结束

可在C可扩展指令块中使用的Python控制代码包括:

当开始,使用 C 可扩展指令块向上爬升、向下降落或悬停。#
当开始
[Climb up or down with buttons 5 and 6.]
起飞至 [500] [毫米 v] ▶
永久循环
if [if controller.button5.pressing():] :: custom-switch-expand
爬升 [向上 v]
else [elif controller.button6.pressing():] :: custom-switch-expand
爬升 [向下 v]
else [else:] :: custom-switch-expand
徘徊
结束
结束

布尔值#

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>

布尔值指令块可以包含Python条件。

带有 Python 条件的布尔指令块#
<[controller.button7.pressing()] :: custom-switch>

布尔指令块也可以使用逻辑运算符:

  • and

  • [or] (…/python/Logic/Operators.md#logical)

  • [not] (…/python/Logic/Operators.md#logical)

带有 Python 逻辑运算符的布尔指令块#
<[controller.button5.pressing() and controller.button7.pressing()] :: custom-switch>

布尔指令块可以使用比较运算符来比较值:

  • 大于: [>] (…/python/Logic/Operators.md#comparison)

  • 小于: [<] (…/python/Logic/Operators.md#comparison)

  • 等于: [= =] (…/python/Logic/Operators.md#comparison)

  • 其他比较运算符包括[< =, ! =, and > =] (…/python/Logic/Operators.md#comparison)

带有 Python 比较运算符的布尔指令块#
<[drone.inertial.get_heading()> 100] :: custom-switch>
当开始,使用布尔指令块重复执行,直到按下按钮 7。#
当开始
[Move with the controller until button 7 is pressed.]
起飞至 [500] [毫米 v] ▶
重复直到 <[controller.button7.pressing()] :: custom-switch>
使用遥控器移动
结束
土地 ▶

报告#

报告器 指令块提供了一个用于编写返回值的 Python 代码的空间。它可以放置在带有循环输入的代码块内。

报告器指令块#
([] :: custom-switch)

报告器 指令块可以包含返回值的 Python 代码。

包含返回值的 Python 代码的报告器指令块#
([drone.get_altitude()] :: custom-switch)

数学运算符可以在报告器指令块中使用:

  • 加法: [+] (…/python/Logic/Operators.md#arithmetic)

  • 减法: [-] (…/python/Logic/Operators.md#arithmetic)

  • 除法: [/] (…/python/Logic/Operators.md#arithmetic)

  • 乘法: [*] (…/python/Logic/Operators.md#arithmetic)

带有 Python 添加功能的报告器指令块#
([drone.get_altitude() + 45] :: custom-switch)
当开始,在无人机爬升 3 秒后打印其高度。#
当开始
[Display the altitude after climbing for 3 seconds.]
起飞至 [500] [毫米 v] ▶
爬升 [向上 v]
等待 [3] 秒
在屏幕上打印 ([drone.get_altitude()] :: custom-switch)▶
土地 ▶

帽子#

帽子 指令块提供了一个定义 Python 函数的空间。附加在 帽子 下的指令块将成为该函数的主体。

帽块#
[] :: hat custom-switch

在 帽 指令块中输入 Python 函数定义。参数放在圆括号内。如果函数不使用参数,则使用空圆括号。

带有 Python 函数定义的帽子模块#
[def function_name(parameter):] :: hat custom-switch
当开始,使用帽块来定义和调用函数。#
[def take_off_500():] :: hat custom-switch
[Define a custom function using a Hat block.]
起飞至 [500] [毫米 v] ▶

当开始
[take_off_500()] :: custom-switch