控制#

介绍#

控件包括用于计时、程序流程、条件逻辑和项目终止的方法。这些控件允许您暂停执行、创建循环、定义逻辑路径以及结束程序。以下是可用控件的列表,包括方法和核心 Python 关键字:

  • wait – Pauses execution for a given number of milliseconds or seconds.

  • for – Repeats code for each item in a sequence.

  • if – Executes code if a condition is true.

  • if/else – Runs different code depending on a condition.

  • if/elif/else – Checks multiple conditions in order.

  • while – Repeats code while a condition is true.

  • break – Exits a loop immediately.

  • stop_program – Ends the running program.

  • pass – Placeholder used when no action is needed.

等待#

wait 暂停特定时间后再转到下一个方法。

用法:
wait(time, units)

参数

描述

time

等待的时间量,以正整数表示。

units

毫秒 MSEC (默认)或 SECONDS

# Build Used: Super Code Base 2.0
def main():
    # Turn right for one second
    drivetrain.turn(RIGHT)
    wait(1, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

为了#

for 遍历一个序列(例如列表、元组、字典、集合或字符串)或任何可迭代对象。它对序列中的每个项目执行一次代码块。

使用量:

for value in expression_list:
    pass

参数

描述

value

存储迭代中的当前元素的临时变量。

expression_list

循环遍历的元素集合(例如,列表、字符串、范围)。

# Build Used: Super Code Base 2.0
def main():
    # Move in a square path
    for index in range(4):
        drivetrain.drive_for(FORWARD, 100, MM)
        drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

如果#

if 如果条件评估为 True则执行缩进的代码块。

用法:

if condition:
    pass

参数

描述

condition

语句运行时要求值的表达式或变量。如果求值为 True,则执行 if 块内的代码;如果求值为 False,则跳过该块。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn_for(RIGHT, 90)
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

如果/否则#

if and else 根据条件评估结果为 TrueFalse确定运行哪个缩进的代码块。

用法:

if condition:
    pass
else:
    pass

参数

描述

condition

语句运行时求值的表达式或变量。如果求值为 True,则执行 if 块内的代码;如果求值为 False,则执行 else 块内的代码。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn(RIGHT)
        else:
            drivetrain.stop()
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

如果/否则#

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

  • if 如果条件评估为 True则运行其块。

  • elif 仅当所有先前条件的评估结果为 False时才检查附加条件。可以使用多个 elif 语句。

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

用法:

if condition:
    pass
elif condition:
    pass
else:
    pass

参数

描述

condition

语句运行时求值的表达式或变量。第一个求值为 True 的条件决定执行哪个块;如果没有一个为 True,则运行 else 块。

# Build Used: Super Code Base 2.0
def main():
    # Change the LED while turning
    drivetrain.turn(RIGHT)
    while True:
        if drivetrain.get_heading() < 120:
            bumper.set_color(OFF)
        elif drivetrain.get_heading() < 240:
            bumper.set_color(RED)
        else:
            bumper.set_color(GREEN)
        wait(0.2, SECONDS)

# Start threads — Do not delete
start_thread(main)

尽管#

while 只要条件为 True就会重复运行方法。它也可以像“等待直到”一样使用,只需在条件中添加 not ,如下例所示。

用法:

while condition:
    pass

参数

描述

condition

每次迭代前都会求值的表达式或变量。如果求值为 True,则循环继续;如果求值为 False,则循环停止。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn(RIGHT)
        else:
            drivetrain.stop()
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

# Build Used: Super Code Base 2.0
def main():
    # Turn the other direction when the bumper is pressed
    drivetrain.turn(LEFT)
    while not bumper.is_pressed():
        wait(0.2, SECONDS)
    drivetrain.turn(RIGHT)

# Start threads — Do not delete
start_thread(main)

休息#

break 立即退出循环。

# Build Used: Super Code Base 2.0
def main():
    # Stop turning after pressing the bumper
    while True:
        drivetrain.turn(RIGHT)
        if bumper.is_pressed():
            break
        wait(0.2, SECONDS)
    drivetrain.stop()
    bumper.set_color(RED)

# Start threads — Do not delete
start_thread(main)

停止程序#

program_stop 结束正在运行的项目。

用法:
brain.program_stop()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Stop the project after pressing the bumper
    while True:
        drivetrain.turn(RIGHT)
        if bumper.is_pressed():
            break
        wait(0.2, SECONDS)
    stop_program()

# Start threads — Do not delete
start_thread(main)

经过#

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

if condition:
    pass

def function():
    pass