控制#

介紹#

Python 中的控制功能可以讓你告訴機器人何時等待、何時重複操作、如何做出決定以及何時結束專案。

以下是可用控制項列表,包括方法和核心 Python 關鍵字:

  • wait – 將項目暫停指定時間。

  • for – 對序列中的每個項目重複程式碼。

  • if – 若條件為真,則執行程式碼。

  • if/else – 根據條件執行不同的程式碼。

  • if/elif/else – 依序檢查多個條件。

  • while – 當條件為真時重複執行程式碼。

  • break – 立即退出迴圈。

  • program_stop – 結束正在執行的程式。

  • pass – 不需要執行任何操作時所使用的佔位符。

等待#

wait 暫停特定時間後再執行下一行程式碼。

Usage:
wait(time, units)

參數

描述

time

等待時間,以正整數或小數表示。

units

表示等待時間的單位: MSEC (預設值) – 毫秒,或 SECONDS

# Move the arm to the desired position and then blink the signal tower
arm.move_to(40, 140, 210, False)
wait(1, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)

為了#

for repeats a set of actions a specific number of times. for can be used to loop through items in lists, tuples, dictionaries, sets, strings, or for a specified amount of loops with range.

使用次數:

for value in expression_list:
    pass

成分

描述

value

一個臨時變量,用於儲存迭代中的當前元素。

expression_list

正在循環遍歷的元素集合(例如,列表、字串、範圍)。

# Move the arm 4 times by 100 millimeters on the y axis
for index in range(4):
    arm.move_inc(0, 100, 0)

如果#

if 運行縮排的程式碼區塊,如果條件為 True。

用法:

if condition:
    pass

成分

描述

condition

語句運行時會檢查的表達式或變數。如果其值為 True, if 區塊內的程式碼;如果其值為 False,則跳過該程式碼區塊。

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    print("\033[2J")
    print(brain.timer_time(SECONDS))
    wait(50, MSEC)
    if arm.is_done():
        brain.timer_reset()

如果/否則#

if 且 else 根據條件是 True 還是 False來決定運行哪個縮排的程式碼區塊。

用法:

if condition:
    pass
else:
    pass

成分

描述

condition

語句運行時會檢查的表達式或變數。如果其值為 True,則執行 if 程式碼區塊中的程式碼;如果其值為 False,則改為執行 else 程式碼區塊中的程式碼。

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        brain.timer_reset()
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

如果/否則#

if/elif/else 結構依條件選擇執行哪個縮排的程式碼區塊:

  • if 運行其程式碼區塊,如果條件為 True。

  • elif 僅當所有先前的條件均為 False時才檢查其他條件。可以使用多個 elif 語句。

  • else 僅當前面的條件都不為 True時才執行其程式碼區塊。

用法:

if condition:
    pass
elif condition:
    pass
else:
    pass

成分

描述

condition

語句運行時會檢查一個表達式或變數。第一個為真 True 的條件決定要執行哪個程式碼區塊;如果沒有為 True),則執行 else( else )程式碼區塊。

# Change the signal tower light if arm can reach to the desired position
while True:
    if arm.can_arm_reach_to(200, 200, 200):
        signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
    elif arm.can_arm_reach_to(300, 300, 300):
        signal_tower.set_color(SignalTower.GREEN, SignalTower.BLINK)
    else:
        signal_tower.set_color(SignalTower.ALL, SignalTower.OFF)
    wait(50, MSEC)

儘管#

while 迴圈執行程式碼,只要條件為 True即可。它也可以像「永遠」一樣使用,條件為 True ;或像「等待直到」一樣使用,條件為 not ,如下例所示。

用法:

while condition:
    pass

成分

描述

condition

每次迭代前都要檢查的表達式或變數。如果其值為 True假),則循環繼續;如果其值為 False假),則循環停止。

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

# Blink blue light until the move is completed
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
arm.move_to(-100, 200, 100, False)
while not arm.is_done():
    wait(0.2, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.ON)

休息#

break 會立即退出迴圈。 2 break 可以用來退出原本會無限循環的循環。

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

程式停止#

program_stop 結束正在執行的項目。

Usage:
brain.program_stop()

參數

描述

此方法沒有參數。

# Stop the project until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)
brain.program_stop()

經過#

pass 是未來程式碼的佔位符,可用來避免空迴圈、條件語句和函數中的錯誤。

if condition:
    pass

def function():
    pass