# Calculating percentage problem

So I have this minor problem that is bothering me atm.
So I have written this code and when I start the script runs fine you can see the hp, totalhp and the percentage. When I reduce my hp it calculates the percentage but it always shows values like 0 and 100.
It’s obvious that those values aren’t correct and I have no idea how to to fix this.

Thx in advance for any help given

``````#Health/ Armor/ Mana/ Skill meters
#Author: Ackou Jens
import direct.directbase.DirectStart

#Values
hp = 0
fullhp = 0
ap = 0
fullap = 0
mp = 0
fullmp = 0
sp = 0
fullsp = 0

#Percentages
hp_100 = 0 #%
ap_100 = 0
mp_100 = 0
sp_100 = 0

#Damage
hpdamage = 0
apdamage = 0
mpdrain = 0
spdrain = 0

#Status
print "hpapmpspMeter.py variable pool created"

#Convert values to a percentage
def convertToPercentage(value_int, totalvalue_int):
newvalue = value_int / totalvalue_int * 100
return newvalue

#Assigns stats on their max
def assignFullStats(fullhp_int, fullap_int, fullmp_int, fullsp_int):
global fullhp
global fullap
global fullmp
global fullsp
global hp
global ap
global mp
global sp

fullhp = fullhp_int
fullap = fullap_int
fullmp = fullmp_int
fullsp = fullsp_int
hp = fullhp_int
ap = fullap_int
mp = fullmp_int
sp = fullsp_int

#Assigns current stats and updates when damaged
def assignStats():
global hp
global fullhp
global hp_100
global ap
global fullap
global ap_100
global mp
global fullmp
global mp_100
global sp
global fullsp
global sp_100

hp = hp - hpdamage
ap = ap - apdamage
ap_100 = convertToPercentage(ap, fullap)
mp = mp - mpdrain
mp_100 = convertToPercentage(mp, fullmp)
sp = sp - spdrain
sp_100 = convertToPercentage(sp, fullsp)

if hp <= 0:
hp = 0
hp_100 = 0
elif hp >= fullhp:
print "Player has reached maximum hp", "[", hp, "/", fullhp, "]"
hp = fullhp
hp_100 = convertToPercentage(hp, fullhp)
else:
hp_100 = convertToPercentage(hp, fullhp)

assignFullStats(5000, 2000, 1000, 100)
assignStats()

print "currentstats are ", "hp=", hp, " ", "fullhp=", fullhp, " ", "hp%=", hp_100

def selfMutilate():
loop = 1
global hp
print "Self Mutilation System Initiated n/"

if loop == 1:
dmg = input("Enter damage amount")
hp = hp - dmg
print "You have mutilated yourself with ", dmg, "damage"
assignStats()
print "currentstats are ", "hp=", hp, " ", "fullhp=", fullhp, " ", "hp%=", hp_100
decision = raw_input("Do you want to continue? (yes/no)")
if decision == "yes":
selfMutilate()
loop = 1
elif decision == "no":
loop = 0

else:
print "Self Mutilation System Terminated"

selfMutilate()

run()``````

The problem lies in the numeric type of your variables: they’re integers, and Python use on the integer division, so 1/10 will give you a 0, 12/10 will be 1.
A simple solution is to use something like that:

``````def convertToPercentage(value_int, totalvalue_int):
newvalue = 100.*value_int / totalvalue_int
return newvalue``````

This way, the interpreter will be using floating numbers calculations.

Thx m8 so putting a “.” after a number will make it a float.

In short: yes.
It’s a lazy way not to write 100.0 or float(100) each time.
Also, you can write 0.234 as .234