小能豆

如何使用函数使用 ANSI 代码格式化打印语句?

py

我知道我可以print(f"\033[32m{newDict[0][1]}\033[0m", f"\033[32m{newDict[1][1]}\033[0m")打印彩色的字典值,

但是我将其放入函数内的 for 循环中,它的输出很奇怪,不是我想要的输出。

输入:

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", "|")

输出:

 | [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 |

我该如何修复这个问题,以便它只打印_每个值/图块?

完整代码:

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)

阅读 6

收藏
2025-01-13

共1个答案

小能豆

你在 boardprint() 函数中使用了 ANSI 转义码来实现彩色输出,但问题是,你的 color 列表值应该是颜色代码(例如,31 代表红色,32 代表绿色),而不是 39(默认前景色),否则输出会显示混乱的 ANSI 转义序列,而不是期望的效果。

问题出在这里:

colorList = [39, 39, 39, 39, 39]

解决方案:

  1. 确保 colorList 的值为有效的颜色代码
    在你的代码中,将 39 替换为具体的颜色代码。例如:
  2. 绿色(正确位置):32
  3. 黄色(错误位置但正确字母):33
  4. 灰色(错误字母):90

  5. 修复 ANSI 转义码显示逻辑
    checkRight() 函数中,根据字母的匹配结果,更新 colorList 中的值。

修复后的代码:

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", "|")

def checkRight(guess, word, row):
    global board, colorList

    updateRow = []
    for x in range(len(word)):
        if guess[x].upper() == word[x]:  # 绿色(正确位置)
            print("green")
            letter = guess[x]
            updateRow.append(str(letter))
            colorList[x] = 32  # 绿色

        elif guess[x].upper() in word:  # 黄色(错误位置但正确字母)
            print("Yellow")
            letter = guess[x]
            updateRow.append(letter)
            colorList[x] = 33  # 黄色

        else:  # 灰色(错误字母)
            print("grey")
            letter = guess[x]
            updateRow.append(letter)
            colorList[x] = 90  # 灰色

    board.update({row: updateRow})
    boardprint(colorList)

    return board, colorList

解释:

  1. colorList 的值设置为有效的颜色代码,确保输出显示正确的颜色。
  2. 根据每个字母的匹配结果,在 checkRight() 中更新 colorList 中的颜色代码。
  3. boardprint() 中,使用更新后的 colorList 渲染颜色。

测试:

运行代码时,控制台将显示类似以下内容:

 | _ _ _ _ _ |
 | A P P L E |
 | _ _ _ _ _ |
 | _ _ _ _ _ |
 | _ _ _ _ _ |
 | _ _ _ _ _ |

其中,字母根据匹配状态显示为绿色、黄色或灰色。

2025-01-13