0

I'm trying to add my health bar into a table display, but I keep getting TypeError: unsupported format string passed to NoneType.__format__ I've looked it up and there's nothing on my specific problem. Here's the code:

def color(r, g, b, text):
    return "\033[38;2;{};{};{}m{} \033[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))

I apologize that it's so long, I didn't know how to make it any shorter. (Also I didn't include the dictionaries with enemy1, enemy2 etc. for length)

I keep getting the error on print("{:<8} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}".format(k, name, level, hpbar, classtype, weapon, armor, effect)) I have yet to find an answer that can solve my problem. It's not the color function as I get the same error when I take the color out.

Note that I am an absolute noob to python and I don't know anything about floats, I just tried it from the answer to another question.

  • 1
    I strongly suspect that one (or more) of *k, name, level, hpbar, classtype, weapon, armor, effect* is None – DarkKnight Jan 27 '22 at 17:15
  • Does this answer your question? [Why is this printing 'None' in the output?](https://stackoverflow.com/questions/28812851/why-is-this-printing-none-in-the-output) or https://stackoverflow.com/questions/7053652/why-is-the-output-of-my-function-printing-out-none – JonSG Jan 27 '22 at 17:34
  • I am back on. The problem is only the implementation of the health bar. Everything else is fine. – iWECHAMPIONSi Jan 27 '22 at 18:24

2 Answers2

0

hpbar is one of the items passed to:

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

Note as well that it is technically the health variable but you are not using that.

However,

hpbar = hp_bar(mxhp,hp,length)

So let's take a look at that.

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

This function does not explicitly return a value and thus implicitly returns None.

Perhaps your print statements should be return statements...

JonSG
  • 10,542
  • 2
  • 25
  • 36
  • 1
    That worked, I just changed the `print` to `return` and now it works. It also helped me find a bug in `hp_bar()`! Thank you! @JonSG – iWECHAMPIONSi Jan 27 '22 at 18:45
0

this work for you ?

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