![](/img/trans.png)
[英]In my code I am having a dilemma with the 'Colorama' module specifically changing my line color using the input() command
[英]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.