0

I'm trying to learn python and as a test i tried to make a BMI calculator

#BMI Calculator

name = raw_input ("What is your name?: ")
weight = raw_input ("Hello %s What is your weight? (kilo): "% (name))
height = raw_input ("And what is your height? (cm) ")

#Calculations: weight / height^2 * 10000

weight = int(weight)
height = int(height)

BMI = (weight / height ** 2) * 10000

print "%s, your BMI is %s"% (name, BMI)

But there seems to be something wrong with the calculations because i always end up with a BMI of 0? whats wrong?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Fred
  • 1

1 Answers1

2

In Python and several languages / is the operator for "integer division." For example, in Python, try:

num = 1 / 2 # Comes out to be 0

Rather, make sure that one of the numbers involved in your division calculation is a float:

num = 1.0 / 2 # Comes out to be 0.5!

In your scenario, since height and weight are integers, it's accidentally doing integer division!

You can also cast variables to floats, like so:

weight_f = float(weight)
poisondminds
  • 427
  • 4
  • 8