为什么彩色模块对我不起作用?

发布于 2025-01-31 16:21:40 字数 806 浏览 4 评论 0原文

我正在尝试使用python中的彩色模块来更改打印功能发出的文本的颜色。我没有收到错误说未检测到该模块的错误,但是我不确定我在做什么错。我有错误代码以及实际代码。

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

现在输入错误消息:

trackback(最近的呼叫最新电话):文件“ [此文件的路径],第30行 in<模块> col = fg(color.upper)文件“ c:\ python310 \ lib \ site-packages \ colored \ colored \ colored.py”,第431行,在FG中 返回彩色(颜色).foreground()文件 “ c:\ python310 \ lib \ site-packages \ colored \ colored.py”,第333行,in 前景 elif self.color.startswith(“#”):attributeError:'hindin_funciton_or_method'对象没有属性'startswith'ps [持有此文件的文件夹]

对不起,如果这是一个微不足道的问题,我仍然刚开始。提前致谢。

代码和错误消息

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2025-02-07 16:21:40

似乎您需要lower()(请注意括号):

from colored import fg

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

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

from colored import fg

color = "Red"
print(fg(color.lower()) + "hello")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文