安慰#

介紹#

VEXcode EXP 控制台顯示在 VEXcode 的監視器視窗中。控制台方法可以在專案執行時顯示文字、數字和變數值。它們還可以清除控制台、讀取用戶輸入以及設定更改顏色後列印文字的顏色。

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

注意: 使用 VEXcode 控制台時,必須先連接到控制台串列埠,然後才能將任何輸出列印到控制台。

以下是所有方法的清單:

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

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

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

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

  • input — 顯示可選的提示字串,等待使用者輸入,並將回應作為文字傳回。

Mutator — 格式化列印文字。

行動#

print#

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

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

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

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

範圍

描述

value

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

end

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

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

VEXcode 控制台,顯示控制台和文字「Hello, robot!」(顯示在第一行)以及「This prints on the next line」(顯示在其下一行)。

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

VEXcode 控制台,顯示控制台和文字「Hello, robot!」。

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

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

VEXcode 控制台,顯示控制台和文字「紅、綠、藍」。

轉義序列#

轉義序列是短代碼,可以用 print 來列印,以改變文字在控制台中的顯示方式。

對於大多數項目,可以使用 set_console_text_color 來更改文字顏色,使用 clear_console 來清除控制台。如果您想在 print 語句內部變更文字顏色或清除控制台,可以使用轉義序列。 print

您可以透過列印 \033 後跟顏色代碼來變更文字顏色。

顏色

轉義序列

紅色的

\033[31m

綠色的

\033[32m

藍色的

\033[34m

黑色的

\033[30m

白色的

\033[37m

黃色的

\033[33m

橘子

\033[91m

紫色的

\033[35m

青色

\033[36m

透明的

\033[97m

# Print VEXcode in red
print("\033[31mVEXcode")

# Print text in different colors
print("Default text color")
print("\033[31m")
print("Red text color")

VEXcode 控制台,顯示控制台和藍色文字「預設文字顏色」(第一行),紅色文字「紅色文字顏色」(第二行)。

若要使用轉義序列清除整個控制台,請列印 \033[2J。這也會將遊標移回第 1 行。

# Clear the Console after two seconds
print("VEXcode")
wait(2, SECONDS)
print("\033[2J")

clear_console#

clear_console 清除控制台中的所有行。

Usage:
clear_console()

參數

描述

此方法沒有參數。

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

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?")
print("Hello, " + answer)

VEXcode 控制台,顯示控制台和文字“你的名字是什麼?”,後面跟著“>>VEX”,下方是“你好 VEX”

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

VEXcode 控制台,顯示控制台和文字“輸入一個數字:”,後面跟著“>> 4”,下方是“5.0”

變異體#

set_console_text_color#

set_console_text_color 設定使用此方法後控制台中列印的文字顏色。在專案開始時,控制台文字顏色設定為 BLUE。

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

Usage:
set_console_text_color(color)

範圍

描述

color

控制台打印文本使用的颜色:

  • Color.BLACK
  • Color.BLUE
  • Color.CYAN
  • Color.GREEN
  • Color.ORANGE
  • Color.PURPLE
  • Color.RED
  • Color.TRANSPARENT
  • Color.WHITE
  • Color.YELLOW

# Print text in different colors
print("Default text color")
set_console_text_color(Color.RED)
print("Red text color")

VEXcode 控制台,顯示控制台和藍色文字「預設文字顏色」(第一行),紅色文字「紅色文字顏色」(第二行)。