Need help with colored text in array

So I’m trying to make wordle, and printing out the keyboard with the different text colors of the status of letters, but my arrays just print out the actual text describing the colors, instead of the colored text

keyboard1 = [("\033[47m", 'Q'), ("\033[47m", 'W'), ("\033[47m", 'E'), ("\033[47m", 'R'), ("\033[47m", 'T'), ("\033[47m", 'Y'), ("\033[47m", 'U'), ("\033[47m", 'I'), ("\033[47m", 'O'), ("\033[47m", 'P')]

:wave: Welcome to the forum @MichaelQu3!

Could you please give us more information, such as the link to your Repl or the full code?

I had to replace all the s you were using with "

Your version:

And my shortened version:

for item in keyboard1:
  for thing in item:
    if thing  == "\033[47m":
      print(thing, end = "")
    else:
      print(thing)

image.

This works! Thank you so much! Also I’m sorry for making you have to replace all the ", I think that happened when I copied my code over.

1 Like

You’re welcome and it’s okay.

Here’s a brief rundown just to show my thought process and to help understand what each lines purpose is for.

First I tried just

keyboard1 = [("\033[47m", "Q"),("\033[47m", "W")]

print(keyboard1)

which outputs [('\x1b[47m', 'Q'), ('\x1b[47m', 'W')]

I tried using eval(), print(eval(keyboard1)) to interpret “\033[47m” as code instead of as a string but got the error

print(eval(keyboard1))
TypeError: eval() arg 1 must be a string, bytes or code object

So then I realized we needed to use a for loop

for item in keyboard1:
  print(item)

this outputs

('\x1b[47m', 'Q')
('\x1b[47m', 'W')

So then I realized we needed another loop for each tuple, ('\x1b[47m', 'Q') and ('\x1b[47m', 'W')

for item in keyboard1:
  for thing in item:
    print(thing)

This itself will work, it outputs :
image

So the rest of the code remaining now, is just to eliminate the unncessary spaces/lines where it’s printing “\033[47m” on the line, but we as the user don’t see anything.

for item in keyboard1:
  for thing in item:
    if thing  == "\033[47m":
      print(thing, end = "")
    else:
      print(thing)

image

I hope this makes the point of everything understandable.


If you’d rather have the letters output horizontally on one line like :
image
instead of vertically, the code can be condensed down to

for item in keyboard1:
  for thing in item:
    print(thing, end ="")

Why bother with a list of tuples at all?

keyboard1 = ["Q", "W"]
for string in keyboard1:
    print(f"\033[47m{string}")

or horizontal:

for string in keyboard1:
    print("\033[47m", end=string)
1 Like

I thought that they might eventually add other distinct colors for one or a few of the letters but yeah if they are all just going to be “\033[47m” then your way is probably better.

1 Like

Oh, obviously. I think I forgot what the original topic was asking.

It could just be a normal list including the escape color code.

keyboard1 = ["\033[47m", "Q", "W"]

for item in keyboard1:
  print(item, end="")

Then if and when you want to add a different color you just add it to the list before the letter(s) you want to be that new color.

keyboard1 = ["\033[47m", "Q", "W", 
             "\033[0m", "A",
             "\033[92m", "B", "C",  
             "\033[47m", "D"]

for item in keyboard1:
  print(item, end="")

image

I think the original version with the tuples could help with reading clarity somewhat, they all have their ups and downs.

1 Like

To shorten it more lol:

for item in keyboard1:
    print(end=item)
1 Like

Yeah that’s just a Discourse tampering with code thing, I’ll wrap the OP’s code so it doesn’t confuse someone else.

2 Likes