1

I know I can do print(f"\033[32m{newDict[0][1]}\033[0m", f"\033[32m{newDict[1][1]}\033[0m") to print a dictionary value in color,

But I'm putting that into a for loop inside a function and its output is weird and not the output for what I want.

INPUT:

board = {
    6 : ["_", "_", "_", "_", "_"],
    5 : ["_", "_", "_", "_", "_"],
    4 : ["_", "_", "_", "_", "_"],
    3 : ["_", "_", "_", "_", "_"],
    2 : ["_", "_", "_", "_", "_"],
    1 : ["_", "_", "_", "_", "_"]
}


def boardprint(color):
    global board

    for x in range(6):
        print(" |", f"\033[{color[0]}m{board[x + 1][0]}\033[0m", f"\033[{color[1]}m{board[x + 1][1]}\033[0m", f"\033[{color[2]}m{board[x + 1][2]}\033[0m", f"\033[{color[3]}m{board[x + 1][3]}\033[0m", f"\033[{color[4]}m{board[x + 1][4]}\033[0m", "|")

OUTPUT:

 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |
 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |
 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |
 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |
 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |
 | [39m_[0m [39m_[0m [39m_[0m [39m_[0m [39m_[0m |

How do I fix this so that it just prints _ for each value/tile?

FULL CODE:

from rich import print
import random

testList = ["PRICE"]
word = random.choice(testList)


board = {
    6 : ["_", "_", "_", "_", "_"],
    5 : ["_", "_", "_", "_", "_"],
    4 : ["_", "_", "_", "_", "_"],
    3 : ["_", "_", "_", "_", "_"],
    2 : ["_", "_", "_", "_", "_"],
    1 : ["_", "_", "_", "_", "_"]
}


def boardprint(color):
    global board

    for x in range(6):
        print(" |", f"\033[{color[0]}m{board[x + 1][0]}\033[0m", f"\033[{color[1]}m{board[x + 1][1]}\033[0m", f"\033[{color[2]}m{board[x + 1][2]}\033[0m", f"\033[{color[3]}m{board[x + 1][3]}\033[0m", f"\033[{color[4]}m{board[x + 1][4]}\033[0m", "|")




guess = ""
def ask():
    global guess
    print("hello! welcome to [bold green]WORDLE[/bold green]")
    print("\tTry to guess a 5-letter word in 6 guesses or less.\n\t\tIf a letter is [green]green[/green], then it is in the right place,\n\t\tIf a letter is [yellow]yellow[/yellow], then it is in the wrong place, \n\t\tif a letter is [black]grey[/black], then it is the wrong letter.")
    print()
    guess = input("FIRST GUESS  > ").upper()
    guess = list(guess)

    return guess


colorList = [39, 39, 39, 39, 39]
def checkRight(guess, word, row):
    global board, colorList

    GuessCheckDouble = {i:guess.count(i) for i in guess}
    WordCheckDouble = {i:word.count(i) for i in word}
    result = {key: GuessCheckDouble[key] - WordCheckDouble.get(key, 0) for key in GuessCheckDouble.keys()}
    updateRow = []

    for x in range(len(word)):
        if guess[x].upper() == word[x]: # green
            print("green")
            
            letter = guess[x]
            updateRow.append(str(letter))
            colorList[x] = 39

        elif guess[x].upper() in word and result[guess[x]] == 0: # yellow
            print("Yellow")
            
            letter = guess[x]
            updateRow.append(letter)
            
        else: # grey
            print("grey")

            letter = guess[x]
            updateRow.append(letter)

    board.update({row : [updateRow[0], updateRow[1], updateRow[2], updateRow[3], updateRow[4]]})
    boardprint(colorList)



    return board, colorList

word = word.upper()
word = list(word)

boardprint(colorList)
ask()
print(guess)

checkRight(guess, word, 1)
print(word)
print(board)
boardprint(colorList)
edwardvth
  • 41
  • 3

1 Answers1

1

For your specific purposes, you don't need rich, and its print function is actually causing your problems because by itself, it seems to mangle the ANSI sequences.

You can do this with Python built-ins only:

board = {
    6 : ["_", "_", "_", "_", "_"],
    5 : ["_", "_", "_", "_", "_"],
    4 : ["_", "_", "_", "_", "_"],
    3 : ["_", "_", "_", "_", "_"],
    2 : ["_", "_", "_", "_", "_"],
    1 : ["_", "_", "_", "_", "_"]
}

def boardprint(colors):
    global board

    final = []
    for x in range(len(board)):
        spots = [f"\033[{col}m{board[x+1][idx]}\33[0m" for idx,col in enumerate(colors)]
        l = ' '.join(spots)
        final.append(f"| {l} |")
    print('\n'.join(final))

colors = [31, 31, 31, 31, 31]
boardprint(colors)

And if your terminal/console recognizes these codes, you'll get something like this (changed it to red code since your 39 code is just a color-reset.

board image with red


If you want to use the rich module, it's preferable to just use their color codes.

import rich

board = {
    6 : ["_", "_", "_", "_", "_"],
    5 : ["_", "_", "_", "_", "_"],
    4 : ["_", "_", "_", "_", "_"],
    3 : ["_", "_", "_", "_", "_"],
    2 : ["_", "_", "_", "_", "_"],
    1 : ["_", "_", "_", "_", "_"]
}

def rich_boardprint(colors):
    global board

    final = []
    for x in range(len(board)):
        spots = [f"[{col}]{board[x+1][idx]}[/]" for idx,col in enumerate(colors)]
        l = ' '.join(spots)
        final.append(f"| {l} |")
    rich.print('\n'.join(final))

rich_colors = ["red", "yellow", "blue", "magenta", "green"]
rich_boardprint(rich_colors)

And this will look like this:

enter image description here


If you want to continue using the ANSI codes as-is, but with rich.print, you can use some additional features of the rich library to translate your ANSI text to something it understands, using rich.text.Text.from_ansi, like this:

import rich
from rich.text import Text

board = {
    6 : ["_", "_", "_", "_", "_"],
    5 : ["_", "_", "_", "_", "_"],
    4 : ["_", "_", "_", "_", "_"],
    3 : ["_", "_", "_", "_", "_"],
    2 : ["_", "_", "_", "_", "_"],
    1 : ["_", "_", "_", "_", "_"]
}

def ansi_rich_boardprint(colors):
    global board

    final = []
    for x in range(len(board)):
        spots = [f"\033[{col}m{board[x+1][idx]}\33[0m" for idx,col in enumerate(colors)]
        l = ' '.join(spots)
        final.append(f"| {l} |")
    ansi_rich = Text.from_ansi('\n'.join(final))
    rich.print(ansi_rich)

colors = [31, 31, 31, 31, 31]
ansi_rich_boardprint(colors)

And you'll get an image like the first example that only uses built-in print:

enter image description here

wkl
  • 77,184
  • 16
  • 165
  • 176