In this article, we are going to create a BMI calculator using the PyWebIO module. This is a python module mostly used to create simple and interactive interfaces on the web using Python programming. It can be installed using the below command:
pip install pywebio
Before creating a calculator let’s take a brief understanding of BMI, The Body Mass Index (BMI) is the value obtained from dividing the weight of a person in Kg by its height square. The SI unit of BMI is kg/m2. Body Mass Index is directly proportional to the mass of an individual and inversely proportional to the square of the height of the individual.
Formula:
BMI = Mass of person / (height)2
where,
Mass in Kilograms(Kg), height in meters(m)
Now, we create a Calculator of BMI using Python and a very interesting library PyWebIO.
Stepwise Implementation:
Import the required modules. For using the python library PyWebIO we have to import some important modules from this library:
Python
from pywebio. input import *
from pywebio.output import *
|
As we can see in the above python program that we first import the required modules from the PyWebIO library. Then we create a class calculation in which we create BMIcalculator() method classifies a person based on the BMI passed as parameter
Python
class calculation:
def BMIcalculator( self , Height, Mass):
BMI = (Mass) / (Height * Height)
for t1, t2 in [( 16 , 'severely underweight' ),
( 18.5 , 'underweight' ),
( 25 , 'normal' ), ( 30 , 'overweight' ),
( 35 , 'moderately obese' ),
( float ( 'inf' ), 'severely obese' )]:
if BMI < = t1:
put_text( 'Your BMI is' , BMI, 'and the person is :' , t2)
break
|
After that from the user we take two inputs as for calculation we need height, mass, and then we calculate the result BMI by using the BMI formula and pass them as parameters in BMIcalculator() which computes BMI and classifies the weight category as per as result of BMI.
Python
Height = input ( "Please enter height in meters(m)" , type = FLOAT )
Mass = input ( "Please enter Mass/Weight in Kilograms(Kg)" , type = FLOAT )
obj = calculation()
obj.BMIcalculator(Height, Mass)
|
Below is the complete program:
Python
from pywebio. input import *
from pywebio.output import *
class calculation:
def BMIcalculator(Height, Mass):
for t1, t2 in [( 16 , 'severely underweight' ),
( 18.5 , 'underweight' ),
( 25 , 'normal' ),
( 30 , 'overweight' ),
( 35 , 'moderately obese' ),
( float ( 'inf' ), 'severely obese' )]:
if BMI < = t1:
put_text( 'Your BMI is' , BMI, 'and the person is :' , t2)
break
class calculation:
def BMIcalculator( self , Height, Mass):
BMI = (Mass) / (Height * Height)
for t1, t2 in [( 16 , 'severely underweight' ),
( 18.5 , 'underweight' ),
( 25 , 'normal' ), ( 30 , 'overweight' ),
( 35 , 'moderately obese' ),
( float ( 'inf' ), 'severely obese' )]:
if BMI < = t1:
put_text( 'Your BMI is' , BMI, 'and the person is :' , t2)
break
Height = input ( "Please enter height in meters(m)" , type = FLOAT )
Mass = input ( "Please enter Mass/Weight in Kilograms(Kg)" , type = FLOAT )
obj = calculation()
obj.BMIcalculator(Height, Mass)
|
Output:


As in the output we take Height (1.7 m) and Mass (60 Kg) at the time of input, and we can see in the output the calculated BMI and their type.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Jul, 2022
Like Article
Save Article