如何将函数中的文本放入 python 中的 table 格式?

How do you put text from a function into the table formatting in python?

我正在尝试将我的健康栏添加到 table 显示中,但我一直收到 TypeError: unsupported format string passed to NoneType.__format__ 我已经查过了,没有关于我的具体问题的任何信息。这是代码:

def color(r, g, b, text):
    return "3[38;2;{};{};{}m{} 3[38;2;255;255;255m".format(r, g, b, text)

def hp_bar(mxhp, hp, length):
    try:
        healthDisplay1 = ''
        dashConvert = int(mxhp/length)
        currentDashes = int(hp//dashConvert)
        remainingHealth = length - currentDashes
        barLength = round(dashConvert/8, 5)
        ramainder = int(hp%dashConvert)
        leftoverBar = barLength * round(ramainder/barLength, 5)
        if leftoverBar >= dashConvert:
            remainingHealth = remainingHealth - 1
            currentDashes = currentDashes + 1
        else:
            leftoverBar1 = round(leftoverBar/barLength)
            if leftoverBar1 == 1:
                healthDisplay1 = '▏' * 1
            if leftoverBar1 == 2:
                healthDisplay1 = '▎' * 1
            if leftoverBar1 == 3:
                healthDisplay1 = '▍' * 1
            if leftoverBar1 == 4:
                healthDisplay1 = '▌' * 1
            if leftoverBar1 == 5:
                healthDisplay1 = '▋' * 1
            if leftoverBar1 == 6:
                healthDisplay1 = '▊' * 1
            if leftoverBar1 == 7:
                healthDisplay1 = '▉' * 1
        healthDisplay = '█' * currentDashes
        remainingDisplay = ' ' * remainingHealth
        red = 127.5/mxhp
        green = 255/mxhp
        red = 250 - round(red*hp)
        green = round(green*hp)
        if green > 255:
            green = 255
        if hp == 0:
            red = 0
        hpdisplay = color(red, green, 0, (healthDisplay + healthDisplay1 + remainingDisplay))
        print('{hp} {:<10}'(color(red, green, 0, (healthDisplay + healthDisplay1 + remainingDisplay))))
    except:
        print(" ")

def attackstat():
    mxhp = float(enemy1['maxhp'])
    hp = float(enemy1['hp'])
    length = float(20)
    hpbar1 = hp_bar(mxhp,hp,length)
    mxhp = float(enemy2['maxhp'])
    hp = float(enemy2['hp'])
    hpbar2 = hp_bar(mxhp,hp,length)
    mxhp = float(enemy3['maxhp'])
    hp = float(enemy3['hp'])
    hpbar3 = hp_bar(mxhp,hp,length)
    mxhp = float(player['maxhp'])
    hp = float(player['hp'])
    hpbar = hp_bar(mxhp,hp,length)
    d = {'1)': [enemy1['name'], enemy1['lvl'], hpbar1, enemy1['class'], enemy1['weapon'], enemy1['armor'], enemy1['effect']],
         '2)': [enemy2['name'], enemy2['lvl'], hpbar2, enemy2['class'], enemy2['weapon'], enemy2['armor'], enemy2['effect']],
         '3)': [enemy3['name'], enemy3['lvl'], hpbar3, enemy3['class'], enemy3['weapon'], enemy3['armor'], enemy3['effect']]}
    c = {'1)': [enemy1['name'], enemy1['lvl'], hpbar1, enemy1['class'], enemy1['weapon'], enemy1['armor'], enemy1['effect']],
         '2)': [enemy2['name'], enemy2['lvl'], hpbar2, enemy2['class'], enemy2['weapon'], enemy2['armor'], enemy2['effect']]}
    b = {'1)': [enemy1['name'], enemy1['lvl'], hpbar1, enemy1['class'], enemy1['weapon'], enemy1['armor'], enemy1['effect']]}
    a = {'Player': [player1['name'], player['lvl'], hpbar, player1['class'], player1['weapon'], player1['armor'], player1['effect']]}
    print(color(0, 255, 0, ("{:<8} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}".format('Ally', 'Name', 'Level','Health', 'Class', 'Weapon', 'Armor', 'Effect'))))
    for k, v in a.items():
        name, level, health, classtype, weapon, armor, effect = v
        print("{:<8} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}".format(k, name, level, hpbar, classtype, weapon, armor, effect))

很抱歉它太长了,我不知道如何让它更短。 (此外,我没有包括 enemy1、enemy2 等的字典的长度)

我一直在 print("{:<8} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}".format(k, name, level, hpbar, classtype, weapon, armor, effect)) 上收到错误 我还没有找到可以解决我的问题的答案。这不是颜色函数,因为我在取出颜色时得到了同样的错误。

请注意,我是 python 的绝对菜鸟,我对浮点数一无所知,我只是从另一个问题的答案中尝试了它。

hpbar 是传递给以下项目之一:

"{:<8} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}".format(k, name, level, hpbar, classtype, weapon, armor, effect)

另请注意,它在技术上是 health 变量,但您没有使用它。

然而,

hpbar = hp_bar(mxhp,hp,length)

所以让我们看一下。

def hp_bar(mxhp, hp, length):
    try:
        # a bunch of code...
        print('{hp} {:<10}'(color(red, green, 0, (healthDisplay + healthDisplay1 + remainingDisplay))))
    except:
        print(" ")

此函数没有显式 return 值,因此隐式 returns None.

也许您的 print 语句应该是 return 语句...

这适合你吗?

# use f'string {value}' format
c = color(red, green, 0, (healthDisplay + healthDisplay1 + remainingDisplay))
print(f'{hp} {c:<10}')