J. Doe
J. Doe

Reputation: 1419

Changing background color and color of text

Say I had a few numbers in a list:

listt = [2, 4, 6]

Is there any way to do something that would change the background color and color of each number depending on which number it is. For example:

for i in range(3):
    if listt[i] == 2:
        # make background color green and make number red
    elif listt[i] == 4:
        # make background color orange and make number green
    elif listt[i] == 6
        # make background color red and make number orange
    print(nlistt[i])

Is there any way to do this and if not with both background and regular color can you do 1 of the 2. Also this should be in the console and not in a new window like pygame.

Upvotes: 2

Views: 2372

Answers (1)

atline
atline

Reputation: 31574

Just apply the color code to what you want to print, the color code could be found in the post @Alex Taylor mentioned.

listt = [2, 4, 6]
nlistt = listt.copy()
for i in range(3):
    if listt[i] == 2:
        # make background color green and make number red
        nlistt[i] = '\033[1;31;42m' + str(nlistt[i]) + '\033[0m'
    elif listt[i] == 4:
        # make background color orange and make number green
        nlistt[i] = '\033[1;32;43m' + str(nlistt[i]) + '\033[0m'
    elif listt[i] == 6:
        # make background color red and make number orange
        nlistt[i] = '\033[1;33;41m' + str(nlistt[i]) + '\033[0m'
    print(nlistt[i])

Upvotes: 2

Related Questions