基本上,我希望能夠提出“你想要什么顏色?”之類的問題。然后讓它根據答案設定一個變數并用于格式化。
所以是這樣的:
print("\033[1;35m\033[1;4m What colour would you like the printed text to be?")
print("\033[1;0m\033[1;1m 1. Red")
print(" 2. Green")
print(" 3. Blue")
ans1 = input()
ans1 = float(ans1)
if ans1 == 1:
colour = 31
print("\033[1;(colour)m This text is red")
elif ans1 == 2:
colour = 32
print("\033[1;(colour)m This text is green")
elif ans1 == 3:
colour = 35
print("\033[1;(colour)m This text is blue")
然后文本將是正確的顏色。這可能嗎?如果可以,我該怎么做?
uj5u.com熱心網友回復:
print("\033[1;35m\033[1;4m What colour would you like the printed text to be?")
print("\033[1;0m\033[1;1m 1. Red")
print(" 2. Green")
print(" 3. Blue")
ans1 = input()
ans1 = float(ans1)
if ans1 == 1:
colour = 31
print(f"\033[1;{colour}m This text is red")
elif ans1 == 2:
colour = 32
print(f"\033[1;{colour}m This text is green")
elif ans1 == 3:
colour = 35
print(f"\033[1;{colour}m This text is blue")
格式化字串文字或 f-string 是前綴為“f”或“F”的字串文字。這些字串可能包含替換欄位,它們是由大括號 {} 分隔的運算式。雖然其他字串文字總是有一個常量值,但格式化字串實際上是在運行時評估的運算式。
uj5u.com熱心網友回復:
也試試這個方法。
from termcolor import COLORS, colored
colour = input('Enter Color name: ').lower()
if colour in COLORS:
print(colored(f"This is {colour} text.",colour))
else:
print("THis color is invalid.")
你的方式。使用f-string.
嘗試這個。
print("\033[1;35m\033[1;4m What colour would you like the printed text to be?")
print("\033[1;0m\033[1;1m 1. Red")
print(" 2. Green")
print(" 3. Blue")
ans1 = input()
ans1 = float(ans1)
if ans1 == 1:
colour = 31
print(f"\033[1;{colour}m This text is red")
elif ans1 == 2:
colour = 32
print(f"\033[1;{colour}m This text is green")
elif ans1 == 3:
colour = 34 # not 35 for blue it is 34
print(f"\033[1;{colour}m This text is blue")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/511267.html
上一篇:添加區域變數和指標變數
