Vincent- Vega
Vincent- Vega

Reputation: 1

How do assign a color to each item in my list using Colorama and print in a matrix?

So,

I'm writing a program that takes random cols and row lengths, a string made up of 5 possible letters and prints a string( made up of random letters) that is equal to the rows X cols. This happens in the first function. then the second takes the string from the first, appends it to a list, and prints it in a matrix. i need to make the second function print each letter according to the name (R needs to be red)

I have the list printing properly but need the content in the list to print colored text using Colorama.

haven't found any info on Colorama interacting with lists, any help would be appreciated. (note: some info in def main is used in later questions)

Code here

#QUESTION 1 WORKING
import random
import colorama
from colorama import init, Fore
init()

def place_random_bricks(m,n,color):
    emp_string = ""
    for x in range(m):
        for y in range(n):
            emp_string += random.choice(color)
    return emp_string


#QUESTION 2


def print_brick_placement(legoString,n):

    for i in range(len(legoString)):
        if i % n == 0:
            sub = legoString [i:i+n]
            lst = []
            for j in sub:
                lst.append(j)
            print(''.join(lst))

        L = [i % 2 for i in range(6)]

        for i in L[::2]:
            L[i] = str(i)

        for item in L: print(Fore.RED + str(item))
        print(Style.RESET_ALL)



def main():
    rows = random.randint(4,9)
    cols = random.randint(4,9)
    allCols = "GRBYC"
    lst = random.sample(range(0, 5), random.randint(3,5))
    colString = ''.join(allCols[i] for i in range(len(lst)))
    colString = ''.join(random.sample(colString,len(colString)))
    print(f'Placing bricks on baseplate with {rows} rows, {cols} cols, and a colour string "{colString}"...')
    legoString = place_random_bricks(rows,cols,colString)
    print("Brick placement (string representation):",legoString)
    print("Brick placement (matrix representation):")
    print_brick_placement(legoString,cols)
    shuffledColString = ''.join(random.sample(colString,len(colString)))
    shuffledColString = shuffledColString.replace("G","")+"G"
    print(Fore.RESET)
    print(f'Rearranging bricks following a colour pattern "{shuffledColString}"...')
    rearrange_bricks(legoString,shuffledColString,rows)
    print(Fore.RESET)
    print(f'The longest consecutive diagonal in the upper triangle is: {find_longest_string(legoString,cols)}')

if __name__ == "__main__":
    main()

Ive tried assigning the color + print followed by the color in functions and for/if loops but no matter where its place in my function it dosent want to work.

any help would be apprecated.

Upvotes: 0

Views: 311

Answers (1)

RMK
RMK

Reputation: 1

I know this is old but I'll answer for future watchers.

I would rather use just raw python. For example, if you want to color each all the R's to red, while leaving other letter as white in a matrix style list try the following:

for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        if matrix[i][j] == "R":
            matrix[i][j] = '\u001b[0;31;40m' + matrix[i][j]
        else:
            matrix[i][j] = '\u001b[0;37;40m' + matrix[i][j]

for row in matrix:
    print(" ".join(row))

For more info on different ways of coloring text in python see https://stackabuse.com/how-to-print-colored-text-in-python/ especially the "16 Colors in Raw Python" part

Upvotes: 0

Related Questions