📜  如何使用Python和 PyWebIO 创建 BMI 计算器 Web 应用程序?

📅  最后修改于: 2022-05-13 01:54:19.691000             🧑  作者: Mango

如何使用Python和 PyWebIO 创建 BMI 计算器 Web 应用程序?

在本文中,我们将使用PyWebIO模块创建一个 BMI 计算器。这是一个Python模块,主要用于使用Python编程在 Web 上创建简单的交互式界面。可以使用以下命令安装它:

pip install pywebio

在创建计算器之前,让我们简要了解一下 BMI,身体质量指数 (BMI) 是一个人的体重(公斤)除以身高的平方所得的值。 BMI的SI单位是kg/m 2 。体重指数与个体的体重成正比,与个体身高的平方成反比。

公式:

BMI = Mass of person /  (height)2 
where,
Mass in Kilograms(Kg), height in meters(m)

现在,我们使用Python和一个非常有趣的库PyWebIO创建了一个 BMI 计算器

分步实施:

导入所需的模块。为了使用Python库 PyWebIO,我们必须从这个库中导入一些重要的模块:



Python
# importing modules
from pywebio.input import *
from pywebio.output import *


Python
# classify and compute BMI
class calculation:
  
    # defining method
    def BMIcalculator(self, Height, Mass):
          
        # compute BMI
        BMI = (Mass)/(Height*Height)
          
        # classify
        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


Python
# height input
Height = input("Please enter height in meters(m)", type=FLOAT)
  
# Mass input
Mass = input("Please enter Mass/Weight in Kilograms(Kg)", type=FLOAT)
  
obj = calculation()
obj.BMIcalculator(Height, Mass)


Python
# importing modules
from pywebio.input import *
from pywebio.output import *
  
# classify person
class calculation:
  
    # defining method
    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
  
# classify and compute BMI
class calculation:
  
    # defining method
    def BMIcalculator(self, Height, Mass):
  
        # compute BMI
        BMI = (Mass)/(Height*Height)
  
        # classify
        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
Height = input("Please enter height in meters(m)", type=FLOAT)
  
# Mass input
Mass = input("Please enter Mass/Weight in Kilograms(Kg)", type=FLOAT)
  
obj = calculation()
obj.BMIcalculator(Height, Mass)


正如我们在上面的Python程序中看到的,我们首先从 PyWebIO 库中导入所需的模块。然后我们创建一个类计算,在其中我们创建BMIcalculator()方法根据作为参数传递的 BMI 对人进行分类

Python

# classify and compute BMI
class calculation:
  
    # defining method
    def BMIcalculator(self, Height, Mass):
          
        # compute BMI
        BMI = (Mass)/(Height*Height)
          
        # classify
        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

之后,我们从用户那里获取两个输入作为计算我们需要身高、体重,然后我们通过使用 BMI 公式计算结果 BMI,并将它们作为参数传递给 BMIcalculator(),它计算 BMI 并根据以下情况对体重类别进行分类BMI 的结果。

Python

# height input
Height = input("Please enter height in meters(m)", type=FLOAT)
  
# Mass input
Mass = input("Please enter Mass/Weight in Kilograms(Kg)", type=FLOAT)
  
obj = calculation()
obj.BMIcalculator(Height, Mass)

下面是完整的程序:

Python

# importing modules
from pywebio.input import *
from pywebio.output import *
  
# classify person
class calculation:
  
    # defining method
    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
  
# classify and compute BMI
class calculation:
  
    # defining method
    def BMIcalculator(self, Height, Mass):
  
        # compute BMI
        BMI = (Mass)/(Height*Height)
  
        # classify
        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
Height = input("Please enter height in meters(m)", type=FLOAT)
  
# Mass input
Mass = input("Please enter Mass/Weight in Kilograms(Kg)", type=FLOAT)
  
obj = calculation()
obj.BMIcalculator(Height, Mass)

输出:

在输出中,我们在输入时采用高度 (1.7 m) 和质量 (60 Kg),我们可以在输出中看到计算出的 BMI 及其类型。