Skip to content
How to make a visual health bar? - Python 3
Profile icon
xolyon

Ok so I have the code here but it is really inefficient ad it doesn't print the whole bar out

here are the problems:

  • not flexible (e.g now only works for 100hp but not 1000
  • "-" don't load in

please fix this either by editing my code or by making your own please send it to me and explain what you did thanks

You are viewing a single comment. View All
Profile icon
BotsBoots

Here is my code below. You can change the values for the total health and the length of the bar and it should still work.

from math import ceil total_health = 100 health = int(input("input health 1-" + str(total_health) + ":")) brb = "x" other_brb = "-" length_bar = 10 #multiply x to represent hp hp = ceil(health / total_health * length_bar) bar = brb * hp end_bar = other_brb * (length_bar - hp) #add bars together print(bar + end_bar)

This is what I changed in your code:

  • I used ceil( ) to round up the number so the bar shows at least one mark until health is zero. You can use round( ) instead if you want, it's up to you.
  • You can make the bar as long as you want by changing the value for length_bar
  • To calculate how many * to print, I did hp = ceil(health / total_health * length_bar). Think of health / total_health being the ratio.

I noticed in the example you use %. Modulus gives the remainder, not the quotient. Use / for division. Have a nice day, I hoped I helped (:

Profile icon
xolyon

Thanks

@Edgod
but one thing:
CN I have as much total health as i want like 23334 and the bar to still be 10 long?

Profile icon
BotsBoots

@xolyon
Yes. Feel free to mess around with my code

Profile icon
xolyon

yes

@Edgod
I just changed total health to an input works well thanks