In: Computer Science
Write a Python program to determine the body-mass index of a collection of six individuals. Your program should include a list of six names. Using a for loop, it should successfully prompt the user for the height in inches and weight in pounds of each individual. Each prompt should include the name of the individual whose height and weight are to be input. It should call a function that accepts the height and weight as parameters and returns the body mass index for that individual using the formula weight × 703 / height2. That body mass index should then be appended to an array. Using a second loop it should traverse the array of body mass indices and call another function that accepts the body mass index as a parameter and returns whether the individual is underweight, normal weight, or overweight. The number of individuals in each category should be counted and the number in each of those categories should be displayed. You should decide on the names of the six individuals and the thresholds used for categorization.
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
# function that accepts the height and weight as parameters and
# returns the body mass index
def calculate_bmi(height, weight):
bmi = weight * (703/(height ** 2))
return bmi
# function that accepts the body mass index as a parameter and
# returns whether the individual is underweight, normal weight, or overweight
def bmi_category(bmi):
#conditions
if ( bmi < 16):
return "underweight"
elif ( bmi >= 16 and bmi <= 25):
return "normalweight"
elif (bmi > 25):
return "overweight"
#program starts here
#creating list of six names
name_list = ["Adam", "Eden", "Tom", "George", "Nina", "Cody"]
bmi_list = [] #declare an empty list for bmi
for name in name_list: #loop through each name in name_list
# getting height and weight for each person
height = float(input(name + ", Enter your height in inches: "))
weight = float(input(name + ", Enter your weight in pounds: "))
bmi = calculate_bmi(height, weight) #calculating the bmi for particular person
bmi_list.append(bmi) #appending the bmi to bmi_list
#initialize number of underweight, normalweight and overweight persons as 0
num_under = 0
num_normal = 0
num_over = 0
for bmi in bmi_list: #loop through each bmi in bmi_list
category = bmi_category(bmi) #finding the category
if category == "underweight": #increment num_under by 1 if category is underweight
num_under = num_under + 1
elif category == "normalweight": #increment num_normal by 1 if category is normalweight
num_normal = num_normal + 1
elif category == "overweight": #increment num_over by 1 if category is overweight
num_over = num_over + 1
#displaying the number of individuals in each category
print("\nNumber of underweight persons: ", num_under)
print("Number of normalweight persons: ", num_normal)
print("Number of overweight persons: ", num_over)
OUTPUT: