安慰#

介绍#

VEXcode 控制台显示在 VEXcode 的监视器窗口中。控制台方法可以在项目运行时显示文本、数字和变量值。它们还可以清除控制台、设置更改颜色后打印的文本颜色,以及监视 VEXcode 监视器选项卡中的变量或传感器值。

使用 VS Code 时,基于文本的控制台输出会显示在终端中,而不是 VEXcode 控制台选项卡中。

以下是所有方法的列表:

操作——输出文本或清空控制台。

  • print — Displays text, numbers, or variable values in the Console or terminal.

  • new_line — Moves the Console cursor to the start of the next row.

  • clear — Clears all rows in the Console.

Getter 方法——读取用户输入。

  • input — Waits for user input and returns the response as text.

Mutator — 格式化打印文本。

行动#

print#

print displays text, numbers, or variable values in the Console. In VS Code, print displays output in the terminal.

如果您希望打印消息中包含项目中的值(例如分数、计时器或传感器读数),请使用自定义字符串格式。有关更多信息,请参阅字符串格式页面。

By default, each brain.print() statement ends with \n, which stands for a new line. This moves the cursor to the next line after the value is printed. The optional end parameter changes what is added after the printed value.

Usage:
brain.print(value, end=“\n”)

参数

描述

value

要在控制台或终端中显示的文本、数字或变量值。

end

Optional. The text added after value is printed. The default is “\n”, which moves the cursor to the next line. Use end=“” to keep the next printed value immediately after the previous value on the same line. Use another string, such as end=”, “, to add text between printed values.

# Display two messages on separate lines
brain.print("Hello, CTE!")
brain.print("This prints on the next line.")

VEXcode 控制台,显示控制台和文本“Hello, CTE!”(显示在第一行)和“This prints on the next line”(显示在其下一行)。

# Display two messages on the same line
brain.print("Hello,", end=" ")
brain.print("CTE!")

VEXcode 控制台,显示控制台和文本“Hello, CTE!”。

# Print three colors on one line, separated by commas
colors = ["Red", "Green", "Blue"]

for color in colors:
    brain.print(color, end=", ")

VEXcode 控制台,显示控制台和文本“红、绿、蓝”。

new_line#

new_line moves the cursor to column 1 on the next row in the Console. The next value printed will appear on that row.

Use this method when you want the next printed value to start on a new row. If this method is not used, and the previous brain.print() statement used end=“” or another value that does not include \n, the next printed value will appear immediately after the previous value on the same row.

Usage:
brain.new_line()

参数

描述

此方法没有参数。

# Print on two lines
brain.print("Row 1", end="")
brain.new_line()
brain.print("Row 2")

VEXcode 控制台,显示控制台,第一行显示文本“第 1 行”,第二行显示文本“第 2 行”。

clear#

clear clears all rows from the Console.

Usage:
brain.clear()

参数

描述

此方法没有参数。

# Display text, then clear it after two seconds
brain.print("This will disappear...")
wait(2, SECONDS)
brain.clear()

Getter#

input#

input displays an optional prompt message, waits for the user to enter a response, then returns that response as text.

The project pauses at input() until a response is entered in the Console. In VS Code, the response is entered in the terminal.

The value returned by input() is always text. To use the response as a number, convert it first with int() or float().

Usage:
input(prompt)

范围

描述

prompt

可选。在等待响应之前,要在控制台或终端中显示的字符串。如果未指定提示符,则在等待之前不显示任何消息。

# Ask for a name, then print a greeting
answer = input("What's your name?")
brain.print("Hello, " + answer)

VEXcode 控制台,显示控制台和文本“你的名字是什么?”,后面跟着“>>VEX”,下方是“你好 VEX”

# Ask for a number, then use it in math
answer = input("Enter a number:")
number = float(answer)
brain.print(number + 1)

VEXcode 控制台,显示控制台和文本“输入一个数字:”,后面跟着“>> 4”,下方是“5.0”

变异体#

set_print_color#

set_print_color sets the color used for text printed in the Console after this method is used. At the start of a project, the Console text color is set to BLACK.

此方法可更改 VEXcode 控制台中的文本颜色。在 VS Code 中,不同终端对颜色的支持可能有所不同。

Usage:
brain.set_print_color(color)

参数

描述

color

The color to use for text printed in the Console:

  • BLACK
  • BLUE
  • GREEN
  • RED

# Print text in different colors
brain.print("Default text color")
brain.new_line()
brain.set_print_color(RED)
brain.print("Red text color")

VEXcode 控制台,显示控制台和黑色文字“默认文本颜色”(第一行),红色文字“红色文本颜色”(第二行)。