gpt4 book ai didi

python - termcolor python 中的占位符

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:33 24 4
gpt4 key购买 nike

我正在处理 CS50 的情绪挑战,我想使用 Termcolor 和占位符在控制台中打印颜色,但我遇到了一些问题。

这是我的代码:

    if score > 0:
green = lambda x: colored(x, 'green')
print(green("1 ", tweets))
elif score < 0:
red = lambda x: colored(x, 'red')
print(red(tweets))
else:
yellow = lambda x: colored(x, 'yellow')
print(yellow(tweets))

我想根据分数(绿色、红色或黄色)打印推文,这没关系,代码适用于 lambda x,但我还想在推文之前以相同颜色打印数字。

我试过 lambda x, y 但我有一个错误:

if score > 0:
green = lambda x, y: colored(x, y, 'green')
print(green("1 ", tweets))


Traceback (most recent call last):
File "./tweets", line 47, in <module>
main()
File "./tweets", line 39, in main
print(green("1 ", tweets))
File "./tweets", line 38, in <lambda>
green = lambda x, y: colored(x, y, 'green')
File "/usr/lib/python3/dist-packages/termcolor.py", line 105, in colored
text = fmt_str % (COLORS[color], text)
KeyError: 'Building Augmented Reality Experiences with Unity3D (and @Microsoft @HoloLens) by @shekitup at @CS50 at @Harvard,'

这是我要打印的内容:

1 + (tweets) in green if positive
-1 + (tweets) in red if negative
0 + (tweets) in yellow if neutral

最佳答案

您可以创建一个子任务并调用它:

def show_tweets_by_color(num, col, tweets):
green = lambda x: colored(x, 'green')
print(colored(str(num), " green") + green(tweets))


if score > 0:
show_tweets_by_color(1, "green", tweets)
...

关于python - termcolor python 中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com