如何更改python中文本的颜色?

时间:2018-10-11 22:02:50

标签: python python-3.x

我制作了一个程序,其中有两个单独的玩家,当玩家浏览我的游戏时,我希望它清楚按颜色区分的玩家得分,例如,我希望Player1的文字为红色,而Player2的文字为蓝色,因为Windows 3上的Python 3没有颜色模块,所以是否可以这样做

  if sum == 1:
    print("Since you rolled an odd you have lost 5 points")
    x=x+1-5

if sum == 2:
    print("Since you rolled an even you have gained 10 points")
    x=x+2+10

if sum == 3:
    print("Since you rolled an odd you have lost 5 points")
    x=x+3-5

if sum == 4:
    print("Since you rolled an even you have gained 10 points")
    x=x+4+10

if sum == 5:
    print("Since you rolled an odd you have lost 5 points")
    x=x+5-5

if sum == 6:
    print("Since you rolled an even you have gained 10 points")
    x=x+6+10

if sum == 7:
    print("Since you rolled an odd you have lost 5 points")
    x=x+7-5

if sum == 8:
    print("Since you rolled an even you have gained 10 points")
    x=x+8+10

if sum == 9:
    print("Since you rolled an odd you have lost 5 points")
    x=x+9-5

if sum == 10:
    print("Since you rolled an even you have gained 10 points")
    x=x+10+10

if sum == 11:
    print("Since you rolled an odd you have lost 5 points")
    x=x+11-5

if sum == 12:
    print("Since you rolled an even you have gained 10 points")
    x=x+12+10

我希望此播放器输出为默认蓝色,但

 if sum == 1:
    print("Since you rolled a odd you have lost 5 points")
    y=y+1-5   

if sum == 2:
    print("Since you rolled a even you have gained 10 points")
    y=y+2-10

if sum == 3:
    print("Since you rolled a odd you have lost 5 points")
    y=y+3-5

if sum == 4:
    print("Since you rolled a even you have gained 10 points")
    y=y+4+10

if sum == 5:
    print("Since you rolled a odd you have lost 5 points")                
    y=y+5-5                                         

if sum == 6:
    print("Since you rolled a even you have gained 10 points")
    y=y+6+10

if sum == 7:
    print("Since you rolled a odd you have lost 5 points")
    y=y+7-5

if sum == 8: 
    print("Since you rolled a even you have gained 10 points")
    y=y+8+10

if sum == 9:
    print("Since you rolled a odd you have lost 5 points")
    y=y+9-5

if sum == 10:
    print("Since you rolled a even you have gained 10 points")
    y=y+10+10

if sum == 11:
    print("Since you rolled a odd you have lost 5 points")
    y=y+11-5

if sum == 12:
    print("Since you rolled a even you have gained 10 points")
    y=y+12+10

此球员的输出是红色或其他任何颜色,请帮忙?

1 个答案:

答案 0 :(得分:1)

您可以使用colorama模块执行此操作:pypi.python.org/pypi/colorama-查看其文档和屏幕截图。

这里有一些示例代码:

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

尽管您将需要在命令行中运行此命令,但是您实际上无法在python shell中执行此操作