Create BMI Calculator using PythonIn the following tutorial, we will understand how to create a Body Mass Index (BMI) Calculator with the help of the Python programming language. But we get started creating one; let us briefly discuss what Body Mass Index (BMI) is. Understanding the Body Mass Index (BMI)BMI, short for Body Mass Index, is a measure of relative weight based on the mass and height of an individual. We generally use the Body Mass Index in order to categorize people on the basis of their height and weight. These categories are underweight, healthy, overweight, and even obesity. Moreover, it is also adopted by various countries in order to promote healthy eating. We can consider Body Mass Index (BMI) as a substitute for direct measurements of body fat. Besides, BMI is a low-cost and easy-to-perform method of screening for weight classes that may cause health-related problems. Understanding the working of BMI CalculatorA BMI Calculator accepts the weight and height of an individual and calculates the Body Mass Index (BMI) of that person. For Example, if the height and weight of a person are 155 cm and 57 kg. The BMI of that person will be 23.73 (approx.), which signifies that the person is healthy. Body Mass Index (BMI) is a measure of body fat on the basis of height and weight, respectively. On the basis of the BMI of an individual, the calculator returns a statement stating the overall health of the person. The following table shows how the classification of BMI is done in order to identify the health status of a person.
Now, let us begin coding the project. Creating BMI Calculator using PythonAs the first step, we will create a New Python program file and name it BMI_Calculator.py. Within this file, we will begin by creating a block of code to ask the user their height and weight. We can easily accomplish this using the input() function. File: BMI_Calculator.py Explanation: In the above snippet of code, we have defined two variables as the_height and the_weight which uses the input() function to accept input from the user. We have also included the float() function outside the input() function in order to convert the input string into the float data type so that we can perform calculations with it. Next, we will calculate the Body Mass Index. We will use the following formula in order to calculate BMI. Let us implement the above formula in the Python program. File: BMI_Calculator.py Explanation: In the above snippet of code, we have defined a function for BMI using the above formula. We have divided the height by 100 to convert the centimeters into meters. Now, let us print the BMI. File: BMI_Calculator.py Explanation: In the above snippet of code, we have printed a statement stating BMI of the person. Now, we will print the statement stating the present health of the user based on their BMI. This block of code will be quite simplified for better understanding. We will use the if-elif-else conditions for classification. File: BMI_Calculator.py Explanation: In the above snippet of code, we have used the value of the variable the_BMI in the if-elif-else statement to check if the BMI of the person lies within one of the categories. The program will print the statement on the following basis:
Hence, the program is completed. Let us see the complete source code for the program and output for the same. Source CodeFile: BMI_Calculator.py Output: Enter the height in cm: 160 Enter the weight in kg: 61 Your Body Mass Index is 23.828124999999996 Awesome! You are healthy. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263425.html