0

I am unable to get a coloured output on the following:

Letters= ['QWERTYUPOIFSAJKVMPFOQWKFWSPAEOOIUSKFISPLCKSKLJHXIWTSLAVW']
data = (Letters)

from colorama import init

init()
init(convert=True)

from colorama import Fore

Selection1 = ['C','L','D','O','I']
Selection2 = ['K','W','Y']
Selection3 = ['N','V']
Selection4 = ['A','G','D']

for x in data:
    if x in Selection1:
        print (Fore.YELLOW , (x))
    if x in Selection2:
        print (Fore.GREEN, (x))
    if x in Selection3:
        print (Fore.LIGHTRED_EX, (x))
    if x in Selection4:
        print (Fore.BLUE, (x))
    else:
        print(Fore.WHITE , (x))

If I change Letters from a list with [] to tuple with (), the output is coloured, but I would prefer to have all the letters as a list in one block rather than individually printed down the page.

1 Answers1

0

If you want to keep the same type, make :

>>> data = list(Letters[0])

To print all characters in same line, pass '' to end parameters of print method:

>>> print(Fore.WHITE, x, end='')

Also, your if-else block needs fixing, since you are checking for almost all your conditions, see below updated code, this way you will be checking only one time for each character:

from colorama import init, Fore

init(convert=True)

Letters= ['QWERTYUPOIFSAJKVMPFOQWKFWSPAEOOIUSKFISPLCKSKLJHXIWTSLAVW']
data = list(Letters[0])

Selection1 = ['C','L','D','O','I']
Selection2 = ['K','W','Y']
Selection3 = ['N','V']
Selection4 = ['A','G','D']

for x in data:
    if x in Selection1:
            print(Fore.YELLOW, x, end='')
    elif x in Selection2:
            print(Fore.GREEN, x, end='')
    elif x in Selection3:
            print(Fore.LIGHTRED_EX, x, end='')
    elif x in Selection4:
            print(Fore.BLUE, x, end='')
    else:
            print(Fore.WHITE, x, end='')
Iron Fist
  • 9,767
  • 2
  • 16
  • 31