簡體   English   中英

我可以使用用戶輸入作為 colorama 模塊中的顏色嗎?

[英]Am i able to use user input as the colour in colorama module?

我正在嘗試使用 colorama 模塊編寫一個程序,該模塊以選定的顏色打印用戶的輸入消息。 除了使用一堆 If 語句之外,還有什么方法可以做到這一點。 這是我目前的代碼:

From Colorama Import Fore, Init 
msg = input("Enter Message here")
color = input("Enter colour (list of all color options)")
color = color.upper()
Print(Fore.color + msg)

現在我的代碼在最后一行給了我一個錯誤,因為顏色不是 Fore 的識別顏色。 關鍵字,但我希望它會使用顏色的用戶輸入。

任何幫助,將不勝感激

如果我現在理解正確,這就是您想要做的:

From Colorama Import Fore, Init 

msg = input("Enter Message here")
color = input("Enter colour (list of all color options)")
color = color.upper()
Print(getattr(Fore, color) + msg)

color變量與Fore.color屬性不同

IE:

  • 設置color意味着你有一個帶有一些字符串的變量
  • 調用Fore.color或獲取屬性只需訪問Fore class 的屬性值

您應該從 Colorama 獲取顏色選項列表作為字符串,然后只需使用color變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM