Skip to content
← Back to Community
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

Answered by BotsBoots [earned 5 cycles]
View Answer
Voters
Profile icon
mkhoi
Profile icon
xolyon
Comments
hotnewtop
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

Profile icon
RedArrow1

I stopped hurting at all when I started ordering https://herbiesheadshop.com/collections/cbd-seeds seeds are here because I know that all CBD products are focused on improving the quality of health and helps you feel better. I highly recommend you to look at the website and get acquainted with the entire range!

Profile icon
TannerTommy

Making visual health bars used to seem like a very technical procedure for me, however, that's not the case anymore though. Ever since I started my research on Bac Water, I've also been researching about this bar. It's pretty good to be honest.

Profile icon
Foster_Bryant

maybe u can make it into a list of some sort and if the user heals append health indicators(s) to the list and if they lose it remove health indicators and have it print the list or array or whatever