安慰#

介紹#

VEXcode 控制台顯示在 VEXcode 的監視器視窗中。控制台方法可以在專案執行時顯示文字、數字和變數值。它們還可以清除控制台、設定更改顏色後列印的文字顏色,以及監視 VEXcode 監視器標籤中的變數或感測器值。

使用 VS Code 時,基於文字的控制台輸出會顯示在終端機中,而不是 VEXcode 控制台標籤中。

以下是所有方法的清單:

操作-輸出文字或清空控制台。

  • print — 在控制台或終端機中顯示文字、數字或變數值。

  • new_line — 將控制台遊標移到下一行的開頭。

  • clear — 清除控制台中的所有行。

Getter 方法-讀取使用者輸入。

  • input — 等待使用者輸入並以文字形式回傳回應。

Mutator — 格式化列印文字。

行動#

print#

print 在控制台中顯示文字、數字或變數值。在 VS Code 中, print 在終端機中顯示輸出。

如果您希望列印訊息中包含項目中的值(例如分數、計時器或感測器讀數),請使用自訂字串格式。有關更多信息,請參閱字串格式頁面。

預設情況下,每個 brain.print() 語句都以 \n。這會在列印值後將遊標移至下一行。可選的 end 參數用於更改列印值之後新增的內容。

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

參數

描述

value

若要在控制台或終端機中顯示的文字、數字或變數值。

end

可選。在 value 之後新增的文字將會被列印。預設值為 “\n”,它會將遊標移到下一行。使用 end=“” 可將下一個列印值緊跟在同一行的前一個值之後。使用另一個字串,例如 end=”, “,可在列印值之間新增文字。

# 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 將遊標移到控制台中下一行的第 1 列。下一個列印的值將顯示在該行。

當您希望下一個列印值從新的一行開始時,請使用此方法。如果未使用此方法,且前 brain.print() 語句使用了 end=“” 或其他不包含 \n\n的值,則下一個列印值將緊跟在同一行的前一個值之後。

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 清除控制台中的所有行。

Usage:
brain.clear()

參數

描述

此方法沒有參數。

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

Getter#

input#

input 顯示可選的提示訊息,等待使用者輸入回應,然後將該回應作為文字傳回。

項目會在執行到 input() 時暫停,直到在控制台中輸入回應。在 VS Code 中,回應會輸入到終端機中。

input() 是文本。要将响应用作数字,请先使用 int() 或 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 設定使用此方法後控制台中列印的文字顏色。在專案開始時,控制台文字顏色設定為 BLACK。

此方法可變更 VEXcode 控制台中的文字顏色。在 VS Code 中,不同終端對顏色的支援可能有所不同。

Usage:
brain.set_print_color(color)

參數

描述

color

控制台列印文字的顏色:

  • 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 控制台,顯示控制台和黑色文字「預設文字顏色」(第一行),紅色文字「紅色文字顏色」(第二行)。