控制#
介紹#
Python 中的控制功能可以讓你告訴機器人何時等待、何時重複操作、如何做出決定以及何時結束專案。
以下是所有控制項的列表,包括方法和核心 Python 關鍵字:
wait– 將項目暫停指定時間。for– 對序列中的每個項目重複程式碼。if– 若條件為真,則執行程式碼。if/else– 根據條件執行不同的程式碼。if/elif/else– 依序檢查多個條件。while– 當條件為真時重複執行程式碼。break– 立即退出迴圈。stop_program– 結束正在執行的程式。pass– 不需要執行任何操作時所使用的佔位符。
等待#
wait 暫停特定時間後再執行下一行程式碼。
用法:wait(time, units)
參數 |
描述 |
|---|---|
|
等待時間,以正整數或小數表示。 |
|
表示等待時間的單位: |
# Move right for one second, then stop
robot.move_at(90)
wait(1, SECONDS)
robot.stop_all_movement()
為了#
for 重复一组特定次数的操作。 for 可用于循环遍历列表、元组、字典、集合、字符串中的项目,或用于具有range的指定次数的循环。
使用次數:
for value in expression_list:
pass
成分 |
描述 |
|---|---|
|
一個臨時變量,用於儲存迭代中的當前元素。 |
|
循環遍歷的元素集合(例如,列表、字串、範圍)。 |
# Move in a square path.
for index in range(4):
robot.move_for(50, 0)
robot.turn_for(RIGHT, 90)
# Print each item in the list
colors = ["Red", "Green", "Blue"]
for color in colors:
robot.screen.print(color)
robot.screen.next_row()

如果#
if 運行縮排的程式碼區塊,如果條件為 True。
用法:
if condition:
pass
成分 |
描述 |
|---|---|
|
語句運行時會檢查的表達式或變數。如果其值為 |
# Kick when the screen is pressed
while True:
if robot.screen.pressing():
robot.kicker.kick(MEDIUM)
wait(0.1, SECONDS)
如果/否則#
if 且 else 根據條件是 True 還是 False來決定運行哪個縮排的程式碼區塊。
用法:
if condition:
pass
else:
pass
成分 |
描述 |
|---|---|
|
語句運行時會檢查的表達式或變數。如果其值為 |
# Show one emoji when the screen is pressed,
# and a different emoji when not pressed
while True:
if robot.screen.pressing():
robot.screen.show_emoji(EXCITED)
else:
robot.screen.show_emoji(BORED)
wait(0.1, SECONDS)
如果/Elif/Else#
if/elif/else 結構依條件選擇執行哪個縮排的程式碼區塊:
if運行其程式碼區塊,如果條件為True。elif僅當所有先前的條件均為False時才檢查其他條件。可以使用多個elif語句。else僅當前面的條件都不為True時才執行其程式碼區塊。
用法:
if condition:
pass
elif condition:
pass
else:
pass
成分 |
描述 |
|---|---|
|
語句運行時會檢查一個表達式或變數。第一個為真 |
# Move the robot forward or reverse
# based on the position of the joystick
def when_axis_changed():
position = controller.axis1.position()
if position > 0:
robot.move_at(0)
elif position < 0:
robot.move_at(180)
else:
robot.stop_all_movement()
controller.axis1.changed(when_axis_changed)
儘管#
while 迴圈執行程式碼,只要條件為 True即可。它也可以像「永遠」一樣使用,條件為 True ;或像「等待直到」一樣使用,條件為 not ,如下例所示。
用法:
while condition:
pass
成分 |
描述 |
|---|---|
|
每次迭代前都要檢查的表達式或變數。如果其值為 |
# Keep the LEDs green while the robot is moving
robot.move_for(200, 0, wait=False)
while robot.is_move_active():
robot.led.on(ALL_LEDS, GREEN)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
while True:
# Continually flash all LEDs red then green.
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
# Wait until the screen is pressed before
# turning off the LEDs
while not robot.screen.pressing():
robot.led.on(ALL_LEDS, RED)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
休息#
break 會立即退出迴圈。 2 break 可以用來退出原本會無限循環的循環。
# Flash LEDs until the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
break
# Turn the LEDs Off
robot.led.off(ALL_LEDS)
停止程式#
stop_program 結束正在運作的項目。
用法:robot.stop_program()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Stop the project once the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
robot.stop_program()
經過#
pass 是未來程式碼的佔位符,可用來避免空迴圈、條件語句和函數中的錯誤。
if condition:
pass
def function():
pass