控制#

等待#

The wait(time, units) command is used to wait for a specific amount of time before moving to the next command.

这是一个等待命令,在命令完成之前不允许执行任何后续命令。

参数

描述

时间

一个正整数,表示下一个命令之前等待的秒数或毫秒数。

单位

The units that the time will use: SECONDS or MSEC(Milliseconds).

**返回:**无。

def main():
    # Drive forward.
    drivetrain.drive(FORWARD)
    # Wait 2 seconds.
    wait(2, SECONDS)
    # Stop the Drivetrain.
    drivetrain.stop()

为了#

for value in expression_list:

for value in expression_list: is a loop that repeatedly executes commands for a number of iterations.

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

价值

报告值的任何整数或先前设置的变量。

表达式列表

The range() function, list, or function that returns a list.

**返回:**无。

def main():
    # Do the following commands 5 times.
    for repeat_count in range(5):
        # Drive forward for 5 inches.
        drivetrain.drive_for(FORWARD, 5, INCHES)
        # Wait 1 second before repeating.
        wait(1, SECONDS)

如果#

if condition:
    # What happens if the condition is True.

The if condition statement is used to execute a block of commands if the condition evaluates as True.

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

状况

条件是任何布尔语句、表达式或变量,其计算结果为真或假。

**返回:**无。

def main():
    # Drive forward continuously.
    drivetrain.drive(FORWARD)

    # Continuously check the if statement every 5 Milliseconds.
    while True:
        wait(5, MSEC)

        # If the left bumper is pressed, turn right for 90 degrees.
        if left_bumper.pressed():
            brain.print("Bumper pressed!")

如果/否则#

if condition:
    # What happens if the condition is True.
else:
    # What happens if the condition is False.

if/else 是一个控制语句,如果条件为 True,则执行一个命令块;如果条件为 False,则执行另一个命令块。

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

状况

条件是任何布尔语句、表达式或变量,其计算结果为真或假。

**返回:**无。

def main():
    # Continuously check the if statement every 5 Milliseconds.
    while True:
        wait(5,MSEC)

        # If the distance sensor has found an object, drive forward.
        if distance.found_object(): 
            drivetrain.drive(FORWARD)

        # If the distance sensor hasn't found an object, turn to the right.    
        else:
            drivetrain.turn(RIGHT)

如果/Elif/Else#

if condition:
    # What happens if condition 1 is True, otherwise, check condition 2.
elif condition2:
    # What happens if condition 2 is True.
else
    # What happens if neither conditions are True.

The elif statement is a conditional that will check if its condition evaluates to True or False. It is possible to combine many conditions with the use of logical operators such as and, or, and not.

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

状况

条件是任何布尔语句、表达式或变量,其计算结果为真或假。

**返回:**无。

You can include as many elif statements in succession as necessary.

def main():
    # Continuously check the if statement every 5 Milliseconds.
    while True:
        wait(5,MSEC)

        # If the Down Eye Sensor detects the color green,
        # turn right for 90 degrees.
        if down_eye.detect(GREEN): 
            drivetrain.turn_for(RIGHT, 90, DEGREES)
        
        # If the Down Eye Sensor detects the color blue,
        # turn left for 90 degrees.
        elif down_eye.detect(BLUE):
            drivetrain.turn_for(LEFT, 90, DEGREES)

        # If the Down Eye Sensor doesn't detect green or blue,
        # drive forward.    
        else:
            drivetrain.drive(FORWARD)

等到#

while not condition:

while not is a loop that repeatedly executes commands as long as the condition evaluates as True.

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

状况

条件是任何布尔语句、表达式或变量,其计算结果为真或假。

**返回:**无。

def main():
    # Continuously check if the VR Robot isn't within 50 MM
    # of an object in front of it every 5 Milliseconds.
    while not front_distance.get_distance(MM) < 50:
        wait(5, MSEC)

        # While the VR Robot is farther than 50 MM of an object,
        # drive forward.
        drivetrain.drive(FORWARD)

    # While the VR Robot is closer than 50 MM of an object, stop driving.
    drivetrain.stop()

尽管#

while condition:

while is a loop that repeatedly executes commands as long as the condition evaluates as True.

这是一个非等待命令,允许任何后续命令无延迟地执行。

参数

描述

状况

条件是任何布尔语句、表达式或变量,其计算结果为真或假。

**返回:**无。

def main():
    # Continuously check if the VR Robot is within 50 MM of an object
    # in front of it every 5 Milliseconds.
    while front_distance.get_distance(MM) > 50:
        wait(5, MSEC)

        # While the VR Robot is farther than 50 MM of an object,
        # drive forward.
        drivetrain.drive(FORWARD)

    # While the VR Robot is closer than 50 MM of an object, stop driving.
    drivetrain.stop()

跳出循环#

The break command is used to exit a repeating loop immediately.

这是一个非等待命令,允许任何后续命令无延迟地执行。

**返回:**无。

def main():
    # Drive forward.
    drivetrain.drive(FORWARD)

    # Continuously check the if statement every 5 Milliseconds.
    while True:
        wait(5,MSEC)

        # If the Front Eye Sensor detects a close object,
        # break out of the while loop.
        if front_eye.near_object():
            break

    # Stop driving if out of the while loop.    
    drivetrain.stop()

停止项目#

The stop_project() command is used to stop a running project.

这是一个等待命令,可防止执行任何后续命令。

**返回:**无。

def main():
    # Continuously check the if statement every 5 Milliseconds.
    while True:
        wait(5,MSEC)

        # Stop the project if the left bumper is pressed.
        if left_bumper.pressed(): 
            stop_project()
        
        # If the left bumper hasn't been pressed, drive forward.
        else:
            drivetrain.drive(FORWARD)