In this article A program that asks for a few kilograms from height and weight to BMI and standard weight I will write.
The body mass index (body mass index) is a body mass index that represents the degree of obesity in humans, which is calculated from the relationship between body weight and height. Generally called BMI (Body Mass Index). Body Mass Index-Wikipedia
Normal body weight is the ideal body weight that is statistically certified to be the healthiest human being, with significantly lower mortality and morbidity over a period of time. , Age, height, body fat percentage, etc., all or part of the factors. standard weight-Wikipedia
BMI ** Standard weight ** Standard weight (kg) = (height (m) x height (m)) x 22
** Create a function to calculate BMI and standard weight ** First, pass two arguments to the function. The first is weight The second is height
Since it is only a few kilograms to display the BMI and the appropriate weight, Current weight-standard weight Then find how many kg (difference).
By the way, the unit of height to be acquired this time is "cm", so it is necessary to return it to "m".
bmi.py
def getBmi(weight, height):
#Convert height from cm to m
height /= 100
bmi = weight / height ** 2
appropriate = height ** 2 * 22
difference = weight - appropriate
** Find and display body shape from BMI ** What is displayed How many kg is left to BMI / body shape / standard weight is. Body types are classified into three types: lean type (<18.5), standard (18.5 <25), and obese type (> = 25). We will conditionally branch this.
bmi(2).py
if bmi < 18.5:
print("BMI: ", '{:.1f}'.format(bmi), "Skinny type up to standard weight", '{:.1f}'.format(difference), "kg increase")
elif bmi < 25:
print("BMI: ", '{:.1f}'.format(bmi), "standard")
elif bmi >= 25:
print("BMI: ", '{:.1f}'.format(bmi), "Obesity up to standard weight", '{:.1f}'.format(difference), "kg weight loss")
The sentence'{: .1f}'.format (bmi) is used to display up to the first decimal place. Also, since it is not necessary to increase or decrease the weight at the time of standard weight, the remaining weight is not displayed.
** Get data and call a function ** Get height and weight in decimal. You can get the decimal number with float (input ()).
bmi(3).py
w = float(input("Please enter your weight.(kg) → "))
h = float(input("Please enter your height.(cm) → "))
getBmi(w, h)
Overall picture
bmi(4).py
def getBmi(weight, height):
height /= 100
bmi = weight / height ** 2
appropriate = height ** 2 * 22
difference = weight - appropriate
if bmi < 18.5:
print("BMI:", '{:.1f}'.format(bmi), "Skinny type up to standard weight", '{:.1f}'.format(difference), "kg increase")
elif bmi < 25:
print("BMI:", '{:.1f}'.format(bmi), "standard")
elif bmi >= 25:
print("BMI:", '{:.1f}'.format(bmi), "Obesity up to standard weight", '{:.1f}'.format(difference), "kg weight loss")
w = float(input("Please enter your weight.(kg) → "))
h = float(input("Please enter your height.(cm) → "))
getBmi(w, h)
Execution result
Please enter your weight.(kg) → 65
Please enter your height.(cm) → 170
→ BMI: 22.5 standard
Please enter your weight.(kg) → 90
Please enter your height.(cm) → 160
→ BMI: 37.1 Obesity 38 to normal weight.7 kg weight loss
This time, A program that asks for a few kilograms from height and weight to BMI and standard weight I wrote.
Basically, I think it's a simple code, so please refer to it.
Thank you very much.
Body Mass Index-Wikipedia standard weight-Wikipedia
Recommended Posts