termcolor 中的占位符 python
placeholder in termcolor python
我正在处理 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
这种方式对您的代码有意义吗?
(此语法仅适用于python >= 3.5,解压后的参数应放在最后,以免之前版本出现歧义)
if score > 0:
green = lambda x: colored(*x, 'green')
print(green(["1 ", tweets]))
你传递一个参数列表,单独推文,或者数字和推文,然后在 lambda 中解压
您也可以创建子任务并调用它:
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)
...
我正在处理 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
这种方式对您的代码有意义吗?
(此语法仅适用于python >= 3.5,解压后的参数应放在最后,以免之前版本出现歧义)
if score > 0:
green = lambda x: colored(*x, 'green')
print(green(["1 ", tweets]))
你传递一个参数列表,单独推文,或者数字和推文,然后在 lambda 中解压
您也可以创建子任务并调用它:
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)
...