Wednesday, August 24, 2022

[FIXED] Why is the colored module not working for me?

Issue

I'm trying to use the colored module in Python to change the color of the text put out by the print function. I'm not receiving an error saying that the module isn't detected, but I'm not sure what I'm doing wrong. I have the error code, as well as the actual code.

11 from colored import fg
...
29 color = input("What is your favorite color?\n")
30 col = fg(color.upper)
31 print(col + "Message")

and now for the error message:

Traceback (most recent call last): File "[this file's path], line 30 in <module> col = fg(color.upper) File "C:\Python310\lib\site-packages\colored\colored.py", line 431, in fg return colored(color).foreground() File "C:\Python310\lib\site-packages\colored\colored.py", line 333, in foreground elif self.color.startswith("#"): AttributeError: 'builtin_funciton_or_method' object has no attribute 'startswith' PS [Folder holding this file]

Sorry if this is a trivial problem, I'm still starting out. Thanks in advance.

Code and error message


Solution

It seems like you need lower() (note the parenthesis):

from colored import fg

color = "Red"
print(fg(color.lower()) + "hello")


Answered By - j1-lee
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.