轉變#
介紹#
Switch 模組幫助學生從基於模組的程式設計過渡到基於文字的程式設計。借助 Switch 模組,學生可以直接在模組專案中輸入 Python 程式碼——編寫命令、條件、值或函數定義——同時仍可使用模組建立專案的其他部分。單一模組也可以轉換為對應的 Python 命令。
若要了解如何輸入 Python 指令、方法和運算符,請參閱 VEX CTE Python API。
由於 Switch 程式碼區塊包含 Python 程式碼,因此拼字、標點符號和縮排必須正確,專案才能按預期運行。
以下是所有 Switch 模組的清單:
切換模組 — 為 Blocks 專案新增 Python 程式碼。
堆疊 — 提供一個空間來鍵入與堆疊區塊依序運行的 Python 指令。
C Block — Provides a space to type Python control code, such as
if,while, orforstatements.C Expandable — 提供一個空間來鍵入具有多個分支的 Python 控製程式碼,例如
if,elif或else。布林值 — 提供一個空間來鍵入傳回
True或False的 Python 程式碼。Reporter — 提供一個空間來鍵入傳回值的 Python 程式碼。
Hat — 提供一個空間來定義 Python 函數。
堆疊#
堆疊 區塊提供了一個空間,用於鍵入與其他堆疊區塊按順序運行的 Python 命令。
[] :: custom-switch
堆疊 區塊可以包含一行或多行 Python 程式碼。
當開始時 :: 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 塊#
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
結束
可在C 程式碼區塊中使用的 Python 控制碼包括:
當開始時 :: 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
在主控台列印 [The 6-Axis Arm can't move to this position.] ◀ 並將游標移至下一列
結束
C 可擴展#
C Expandable 區塊提供了一個空間,用於輸入具有多個分支的 Python 控製程式碼,例如 if elif或 else可以透過選擇加號箭頭來新增其他分支。
if [] :: custom-switch-expand
結束
可在C Expandable程式碼區塊中使用的Python控製程式碼包括:
當開始時
[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
在主控台列印 [The 6-Axis Arm can't move to this position.] ◀ 並將游標移至下一列
else [else:] :: custom-switch-expand
在主控台列印 [The 6-Axis Arm can move to this position.] ◀ 並將游標移至下一列
結束
布林值#
布林值 區塊提供了一個空間,用於輸入傳回 True 或 False的 Python 程式碼。它可以放置在具有六邊形輸入的區塊內。
<[] :: custom-switch>
布林值區塊可以包含Python條件。
<[bumper_a.is_pressed()] :: custom-switch>
布林區塊也可以使用邏輯運算子:
<[bumper_a.is_pressed() or bumper_b.is_pressed()] :: custom-switch>
布林區塊可以使用比較運算子來比較值:
大於:
>小於:
<等於:
==其他比較運算子包括
<=,!=, 和>=
<[drivetrain.get_heading()> 100] :: custom-switch>
當開始時
[Check if the 6-Axis Arm can move to a position.]
如果 <非 <[arm.can_arm_reach_to(0, 0, 0)] :: custom-switch>> 則
在主控台列印 [The 6-Axis Arm can't move to this position.] ◀ 並將游標移至下一列
結束
記者#
Reporter 程式碼區塊提供了一個用於編寫傳回值的 Python 程式碼的空間。它可以放置在具有循環輸入的程式碼區塊內。
([] :: custom-switch)
Reporter 程式碼區塊可以包含傳回值的 Python 程式碼。
([arm.get_y()] :: custom-switch)
數學運算子可以在Reporter模組中使用:
([arm.get_y() + 45] :: custom-switch)
當開始時
[Display the current y-position while the 6-Axis Arm moves.]
移動 [手臂 v] 至位置 x:(-100) y:(200) z:(100) [mm v] ◀ 且不等待
重複直到 <[手臂 v] 移動完成?>
在主控台列印 ([arm.get_y()] :: custom-switch) ◀ 並將游標移至下一列
等待 [0.25] 秒
結束
帽子#
帽子 程式碼區塊提供了一個定義 Python 函數的空間。附加在 帽子 下的程式碼區塊將成為該函數的主體。
[] :: hat custom-switch
在 Hat 程式碼區塊中輸入 Python 函數定義。參數放在圓括號內。如果函數不使用參數,則使用空圓括號。
[def function_name(parameter):] :: hat custom-switch
[def increment_y_100():] :: hat custom-switch
[Create a function to increment the 6-Axis Arm.]
將 [手臂 v] 位置遞增 x:[0] y:[100] z:[0] [mm v] ▶
當開始時 :: hat events
[increment_y_100()] :: custom-switch