Hey guys,

Is it possible to change the color of characters in a list if they do/dont meet certain conditions? So here's my code:

def checkWord(real, guess, remReal, remGuess, wrongSpot):
    # Format the letters so user knows whats wrong/right
    fullWord= real
            
    # Create copy list of real word
    realWord= list(fullWord)

    # Create index start point
    index= 0

    # Iterate for each letter in 'guess'
    for letter in guess:
        #print(letter)

        # Print letter with '[]' if it is in the wrong positon list 
        if letter in wrongSpot:
            print('[',letter,']',end='')
            # Convert letter in list to a '*'
            realWord[index]= '*'
            # Add to accumulator value
            index+=1
            # Debug
            #print (realWord)
            
        # If the letter is one of the remaining letters in the guess list but
        # it's not in the current word list so it is not in the real word,
        # print only the letter
        elif letter in remGuess and letter not in realWord:
            print(letter,end='')
            realWord[index]= '*'
            index+=1
            #print (realWord)

        # Print letter with '()' if letter is in the current word list
        elif letter in realWord:
            print('(',letter,')',end='')
            realWord[index]= '*'
            index+=1

I want the LIST characters to be changed to yellow if the first if condition is met, red and italic if the second elif condition is met, and green and bold if the last elif is met. Is there a way to go about doing this where the output prints into the shell?

Recommended Answers

All 2 Replies

It depends on your output shell. If you are using python in a terminal, you can print in colors with this module http://pypi.python.org/pypi/colorama . On the other hand, it won't work if you are using idle or another IDE with a graphical user interface (each GUI widget has its own rules to display text in colors, and idle for example has no user API to do this).

It depends on your output shell. If you are using python in a terminal, you can print in colors with this module http://pypi.python.org/pypi/colorama . On the other hand, it won't work if you are using idle or another IDE with a graphical user interface (each GUI widget has its own rules to display text in colors, and idle for example has no user API to do this).

Ok thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.