0

I want to change the color of the letters in a list of words. I use colorama library. Code:

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
li,ly = [],[]
for word in guess:
    for i in range(len(word)):
        if word[i] == 'b' or word[i] == 'a':
            color = Fore.RED
        elif word[i] == 's' or word[i] == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        letter = color + word[i] + Fore.RESET
        li.append(letter)
    result = ' '.join(li)
    ly.append(result)
print(*ly, sep = '\n')

Output is

g r e a t

g r e a t h e l l o

g r e a t h e l l o b r a v e

g r e a t h e l l o b r a v e s t o n e

Desired output would be

g r e a t

g r e a t

h e l l o

g r e a t

h e l l o 

b r a v e

g r e a t 

h e l l o 

b r a v e 

s t o n e

How do I add a new line in a nested for loop? I have not found a solution. Please help.

0

3 Answers 3

1

enter image description here

Not sure if this is the exact solution you are looking for, but I was able to print them like this by appending a "\n"

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
li,ly = [],[]
for word in guess:
    for i in range(len(word)):
        if word[i] == 'b' or word[i] == 'a':
            color = Fore.RED
        elif word[i] == 's' or word[i] == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        letter = color + word[i] + Fore.RESET
        li.append(letter)
    li.append("\n")
    result = ''.join(li)
    ly.append(result.rstrip())
    
print(*ly, sep = '\n')
Sign up to request clarification or add additional context in comments.

Comments

0

This should do it:

guess = ['great', 'hello', 'brave', 'stone']
color_letters = []
spaced_words = []

for word in guess:
    # insert spaces between letters
    for letter in ' '.join(word):

        # identify colour to apply
        if letter in 'ab':
            color = Fore.RED
        elif letter in 'es':
            color = Fore.GREEN
        else:
            color = Fore.BLACK

        # append to cumulative list of formatted letters
        color_letters.append(color + letter + Fore.RESET)

    # insert line breaks between words
    color_letters += '\n\n'

    # add / re-add cumulative list of letters to output list
    spaced_words.append(''.join(color_letters))

print(''.join(spaced_words))

1 Comment

Yep, it is working. thank you. And your 'in' is way better than my or condition.
0

The problem is that li contains the words without newlines and is not cleared after each iteration, resulting in missing newlines.

A solution would be to clear li after each iteration:

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
text, line = [], []
for word in guess:
    for character in word:
        if character == 'b' or character == 'a':
            color = Fore.RED
        elif character == 's' or character == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        line.append(color + character + Fore.RESET)
    text.append(' '.join(line))
    line.clear()
print(*text, sep = '\n')

If you want the repetitions then create them with a loop:

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
text, line = [], []
for word in guess:
    for character in word:
        if character == 'b' or character == 'a':
            color = Fore.RED
        elif character == 's' or character == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        line.append(color + character + Fore.RESET)
    text.append(' '.join(line))
    line.clear()
for i in range(1, len(guess) + 1):
    print(*text[:i], sep = '\n')

1 Comment

I think your way might also work, thanks for pointing out clear() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.