Variables#
Monitor Variables#
To add a variable’s value to the Monitor Console, use the monitor_variable() command.
Numeric Variable#
Numeric variables are used to store numerical values.
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:
The name cannot use special characters (e.g. an exclamation point).
The name cannot begin with a number.
The name cannot use spaces.
The name cannot be a reserved word in VEXcode VR (e.g. 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)
String Variable#
String variables are used to store strings, which is a sequence of characters. Characters can include numbers, letters, and symbols.
To assign a string variable a value, use the = operator.
The default name of a string variable is my_string
. Apply the following rules when naming a string variable:
The name cannot use special characters (e.g. an exclamation point).
The name cannot begin with a number.
The name cannot use spaces.
The name cannot be a reserved word in VEXcode VR (e.g. 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)
Boolean Variable#
Boolean variables can hold either True or False values.
To assign a boolean variable a value, use the = operator.
The default name of a boolean variable is my_boolean
. Apply the following rules when naming a boolean variable:
The name cannot use special characters (e.g. an exclamation point).
The name cannot begin with a number.
The name cannot use spaces.
The name cannot be a reserved word in VEXcode VR (e.g. 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()
Numeric List#
List variables are used to store multiple values in a single variable name.
The default name of a List variable is: my_list = [0, 0, 0]
When being named, List variables have the same naming rules as all other variables:
The name cannot use special characters (e.g. an exclamation point)
The name cannot begin with a number
The name cannot use spaces
The name cannot be a reserved word in VEXcode VR (e.g. Drivetrain)
If the values are numbers, then the commas should come right after each number.
If the values are strings, the commas should come outside of the quotation marks.
The data elements in a List variable can be accessed using an index.
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)
Numeric 2D List#
Similar to a List variable, 2D List variables are used to store multiple values in a single variable name. However, 2D List variables store more than one list.
The default name of a 2D List variable is:
my_2d_list = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
When being named, 2D List variables have the same naming rules as all other variables:
The name cannot use special characters (e.g. an exclamation point)
The name cannot begin with a number
The name cannot use spaces
The name cannot be a reserved word in VEXcode VR (e.g. 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)