控制#
等待#
wait(time, units)
命令用于等待特定的时间,然后再转到下一个命令。
这是一个等待命令,在命令完成之前不允许执行任何后续命令。
参数 |
描述 |
---|---|
时间 |
一个正整数,表示下一个命令之前等待的秒数或毫秒数。 |
单位 |
时间将使用的单位: |
**返回:**无。
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:
是一个循环,它会重复执行命令多次。
这是一个非等待命令,允许任何后续命令无延迟地执行。
参数 |
描述 |
---|---|
价值 |
报告值的任何整数或先前设置的变量。 |
表达式列表 |
|
**返回:**无。
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.
如果条件计算结果为 True,则使用 if condition
语句来执行一组命令。
这是一个非等待命令,允许任何后续命令无延迟地执行。
参数 |
描述 |
---|---|
状况 |
条件是任何布尔语句、表达式或变量,其计算结果为真或假。 |
**返回:**无。
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.
elif
语句是一个条件语句,用于检查其条件结果为 True 还是 False。可以使用逻辑运算符(例如 and
、or
、and not
)组合多种条件。
这是一个非等待命令,允许任何后续命令无延迟地执行。
参数 |
描述 |
---|---|
状况 |
条件是任何布尔语句、表达式或变量,其计算结果为真或假。 |
**返回:**无。
您可以根据需要连续包含任意数量的“elif”语句。
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
是一个循环,只要条件为 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
是一个循环,只要条件为 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()
跳出循环#
break
命令用于立即退出重复循环。
这是一个非等待命令,允许任何后续命令无延迟地执行。
**返回:**无。
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()
停止项目#
stop_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)