变量#
监视变量#
要将变量的值添加到监视控制台,请使用 monitor_variable() 命令。
数值变量#
数字变量用于存储数值。
To assign a numeric variable a value, use the =
operator.
The default name of a numeric variable is my_number
. Apply the following rules when naming a numeric variable:
名称不能使用特殊字符(例如感叹号)。
名称不能以数字开头。
名称中不能使用空格。
该名称不能是 VEXcode VR 中的保留字(例如 Drivetrain)。
def main():
# Create the numeric variable "drive_distance" and set it to 500.
drive_distance = 500
# Drive forward for the drive_distance's value in Millimeters.
drivetrain.drive_for(FORWARD, drive_distance, MM)
字符串变量#
字符串变量用于存储字符串,即字符序列。字符可以包括数字、字母和符号。
要为字符串变量赋值,请使用 = 运算符。
The default name of a string variable is my_string
. Apply the following rules when naming a string variable:
名称不能使用特殊字符(例如感叹号)。
名称不能以数字开头。
名称中不能使用空格。
该名称不能是 VEXcode VR 中的保留字(例如 Drivetrain)。
def main():
# Create the string variable "new_project".
new_project = "New project has started."
# Print the String value of "new_project".
brain.print(new_project)
布尔变量#
布尔变量可以保存 True 或 False 值。
要为布尔变量赋值,请使用 = 运算符。
The default name of a boolean variable is my_boolean
. Apply the following rules when naming a boolean variable:
名称不能使用特殊字符(例如感叹号)。
名称不能以数字开头。
名称中不能使用空格。
该名称不能是 VEXcode VR 中的保留字(例如 Drivetrain)。
def main():
# Create the boolean variable "reverse_drive".
reverse_drive = True
# Check the Boolean value from the variable "reverse_drive".
if reverse_drive:
# Drive reverse if True.
drivetrain.drive(REVERSE)
else:
# Drive forward if False.
drivetrain.drive(FORWARD)
# Wait 5 seconds.
wait(5, SECONDS)
# Stop the drivetrain.
drivetrain.stop()
数字列表#
列表变量用于在单个变量名中存储多个值。
The default name of a List variable is: my_list = [0, 0, 0]
List 变量的命名规则与所有其他变量相同:
名称不能使用特殊字符(例如感叹号)
名称不能以数字开头
名称不能使用空格
该名称不能是 VEXcode VR 中的保留字(例如 Drivetrain)
如果值是数字,则逗号应紧跟在每个数字后面。
如果值是字符串,则逗号应位于引号之外。
可以使用索引访问列表变量中的数据元素。
def main():
# Create the numeric list "drive_distance".
drive_distance = [100, 200, 300]
# Drive forward for 200 Millimeters.
drivetrain.drive_for(FORWARD, drive_distance[2], MM)
二维数字列表#
与列表变量类似,二维列表变量用于在单个变量名中存储多个值。然而,二维列表变量可以存储多个列表。
2D 列表变量的默认名称是:
my_2d_list = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
2D List 变量的命名规则与所有其他变量相同:
名称不能使用特殊字符(例如感叹号)
名称不能以数字开头
名称不能使用空格
该名称不能是 VEXcode VR 中的保留字(例如 Drivetrain)
The data elements in two dimensional arrays can be accessed using two indexes. An index is the location of a value in the List variable. To access a value, first use the name of the 2D List and then the two indexes enclosed in square brackets: my_2d_list[0][0]
. The first index is the row and the second index is the column, starting at 0
.
def main():
# Create the 2D numeric list "drive_distance".
drive_distance = [
[5, 10],
[15, 20],
]
# Drive forward for 10 inches.
drivetrain.drive_for(FORWARD, drive_distance[0][1], INCHES)