控制#

介绍#

Python 中的控制功能可以让你告诉机器人何时等待、何时重复操作、如何做出决定以及何时结束项目。

以下是可用控件列表,包括方法和核心 Python 关键字:

  • wait — 将项目暂停指定时间。

  • for — 对序列中的每个项目重复代码。

  • if — 如果条件为真,则运行代码。

  • if/else — 根据条件运行不同的代码。

  • if/elif/else — 按顺序检查多个条件。

  • while — 当条件为真时重复执行代码。

  • break — 立即退出循环。

  • stop_program — 结束正在运行的程序。

  • pass — 不需要操作时使用的占位符。

等待#

wait 暂停特定时间后再执行下一行代码。

Usage:
wait(time, units)

参数

描述

time

等待时间,以正整数或小数表示。

units

表示等待时间的单位: MSEC (默认值) – 毫秒,或 SECONDS

# Turn right for one second, then stop
drivetrain.turn(RIGHT)
wait(1, SECONDS)
drivetrain.stop()

为了#

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 in a square path
for index in range(4):
    drivetrain.drive_for(FORWARD, 100, MM)
    drivetrain.turn_for(RIGHT, 90)

# Print each item in the list
colors = ["Red", "Green", "Blue"]

for color in colors:
    brain.screen.print(color)
    brain.screen.next_row()

IQ Brain 屏幕的截图,屏幕左侧有三行白色文字。第一行是“红色”,紧挨着它的第二行是“绿色”,再下面一行是“蓝色”。

如果#

if 运行缩进的代码块,如果条件为 True。

用法:

if condition:
    pass

成分

描述

condition

An expression or variable that is checked when the statement runs. If it is True, the code inside the if block executes; if it is False, the block is skipped.

# Turn right when a button is pressed
while True:
    if brain.buttonCheck.pressing():
        drivetrain.turn_for(RIGHT, 90)
    wait(0.1, SECONDS)

如果/否则#

if 且 else 根据条件是 True 还是 False来确定哪个缩进的代码块运行。

用法:

if condition:
    pass
else:
    pass

成分

描述

condition

语句运行时会检查的表达式或变量。如果其值为 True,则执行 if 代码块中的代码;如果其值为 False,则改为执行 else 代码块中的代码。

# Turn right when a button is pressed, and remain still
# when the button is not pressed
while True:
    if brain.buttonCheck.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()
    wait(0.1, SECONDS)

如果/否则#

if/elif/else 结构根据条件选择执行哪个缩进的代码块:

  • if 运行其代码块,如果条件为 True。

  • elif 仅当所有先前的条件均为 False时才检查其他条件。可以使用多个 elif 语句。

  • else 仅当前面的条件都不为 True时才运行其代码块。

用法:

if condition:
    pass
elif condition:
    pass
else:
    pass

成分

描述

condition

语句运行时会检查一个表达式或变量。第一个为真 True 的条件决定执行哪个代码块;如果没有为 True),则执行 else( else )代码块。

# Turn depending on what button is pressed
while True:
    if brain.buttonRight.pressing():
        drivetrain.turn(RIGHT)
    elif brain.buttonLeft.pressing():
        drivetrain.turn(LEFT)
    else:
        drivetrain.stop()
    wait(0.1, SECONDS)

尽管#

while 循环执行代码,只要条件为 True即可。它也可以像“永远”一样使用,条件为 True ;或者像“等待直到”一样使用,条件为 not ,如下例所示。

用法:

while condition:
    pass

成分

描述

condition

每次迭代前都要检查的表达式或变量。如果其值为 True真),则循环继续;如果其值为 False假),则循环停止。

# Keep the screen green while the robot is moving
drivetrain.drive_for(FORWARD, 200, MM, wait=False)
while drivetrain.is_moving():
    brain.screen.clear_screen(Color.GREEN)
    wait(50, MSEC)

brain.screen.clear_screen(Color.BLACK)

while True:
    # Move in a square path forever
    drivetrain.drive_for(FORWARD, 150, MM)
    drivetrain.turn_for(RIGHT, 90)

# Wait until a button is pressed to stop driving
drivetrain.drive(FORWARD)
while not brain.buttonCheck.pressing():
    wait(50, MSEC)
drivetrain.stop()

休息#

break 会立即退出循环。2 break 可以用来退出原本会无限循环的循环。

# Stop turning after a button is pressed
while True:
    drivetrain.turn(RIGHT)
    if brain.buttonCheck.pressing():
        break
drivetrain.stop()
brain.play_sound(SoundType.POWER_DOWN)

停止程序#

program_stop 结束正在运行的项目。

Usage:
brain.program_stop()

参数

描述

该方法没有参数。

# Stop the project after a button is pressed
while True:
    drivetrain.turn(RIGHT)
    if brain.buttonCheck.pressing():
        brain.program_stop()
    brain.play_sound(SoundType.POWER_DOWN)

经过#

pass 是未来代码的占位符,可用于避免空循环、条件语句和函数中的错误。

if condition:
    pass

def function():
    pass