【发布时间】:2017-07-08 23:40:32
【问题描述】:
我正在处理 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
【问题讨论】: