Bailey17
Bailey17

Reputation: 19

Calculate Body Mass Index

I'm writing a program that will prompt the user for 6 names of individuals who want to calculate their body mass index and then prompt each person for their height and weight. However, I can't seem to figure out how to select a single person from the list I created. It keeps selecting all the entered names. Can someone please advise how I can select just one person at a time? Thanks


print ("This program will help calculate the body mass index of 6 people")
users = str(input("Please enter the names of the 6 users who want to calculate thier BMI: "))
individuals = list()
individuals.append(users)
userA = individuals[0:1]
for userA in individuals:
    height = int(input("In inches, how tall are you? "))
    weight = int(input("In pounds, how much do you weight? "))
    BMI = print(userA, "Your BMI is: ", weight * 703/height**2)

Upvotes: 1

Views: 2168

Answers (5)

kendfss
kendfss

Reputation: 514

This will do it for any number of individuals

bmi = lambda weight, height: weight * 703/height**2
names = input('Enter the names of the user(s), each separated by a comma\n\t').split(',')

df = {
    name.strip():[
        int(input(f"What is {name.strip()}'s weight?\t(pounds)\n\t")),
        int(input(f"What is {name.strip()}'s height?\t(inches)\n\t"))
    ] for name in names
}

for k,v in df.items():
    print(f"{k}'s BMI is {bmi(*v)}")

Upvotes: 0

Asra
Asra

Reputation: 191

Looping six times on the input and all though the list will work, such as:

print ("This program will help calculate the body mass index of 6 people")
individuals = list()
for i in range(6):
    user = str(input("Please enter the names of the 6 users who want to calculate their BMI: "))
    individuals.append(user)
for user in individuals:
    print("Calculating for", user)
    height = int(input(user + ", in inches, how tall are you? "))
    weight = int(input(user + ", in pounds, how much do you weight? "))
    BMI = print(user  + ", your BMI is: ", weight * 703/height**2)

EDIT

Here is a version to store the BMIs in a list

print ("This program will help calculate the body mass index of 6 people")
individuals = list()
for i in range(6):
    user = str(input("Please enter the names of the 6 users who want to calculate their BMI: "))
    individuals.append(user)
BMIs = []
for user in individuals:
    print("Calculating for", user)
    height = int(input(user + ", in inches, how tall are you? "))
    weight = int(input(user + ", in pounds, how much do you weight? "))
    BMIs.append(user  + ", your BMI is: " + str(weight * 703/height**2))

for BMI in BMIs:
    print(BMI)

Upvotes: 3

pawmasz
pawmasz

Reputation: 103

Try this program. Just copy it and run:

print ("This program will help calculate the body mass index of 6 people")

print("Please enter the names of the 6 users who want to calculate thier BMI: ")
individuals = []

for i in range(0, 6):
    user = input()
    individuals.append(user)

for userA in individuals:
    height = int(input("In inches, how tall are you? "))
    weight = int(input("In pounds, how much do you weight? "))
    BMI = print(userA + "Your BMI is: " + weight * 703 / height ** 2)

Upvotes: 0

willwrighteng
willwrighteng

Reputation: 3071

Try this instead

print ("This program will help calculate the body mass index of 6 people")
users = str(input("Please enter the names of the 6 users who want to calculate thier BMI (comma separated): "))

users = users.split(',')
print(users)
userA = users[0]
userA

#This program will help calculate the body mass index of 6 people
#Please enter the names of the 6 users who want to calculate thier BMI (comma separated):  a,b,c,d
#['a', 'b', 'c', 'd']
#'a'

Upvotes: 1

Jane
Jane

Reputation: 923

Would this work?

Testrun:

python BMI.py

Enter height in meters: 1.75
Enter weight in kg: 64
Your BMI is: 20.897959183673468 and you are: Healthy

Python code:

# getting input from the user and assigning it to user

height = float(input("Enter height in meters: "))
weight = float(input("Enter weight in kg: "))

# the formula for calculating bmi

bmi = weight/(height**2) 
# ** is the power of operator i.e height*height in this case

print("Your BMI is: {0} and you are: ".format(bmi), end='')

#conditions
if ( bmi < 16):
   print("severely underweight")

elif ( bmi >= 16 and bmi < 18.5):
   print("underweight")

elif ( bmi >= 18.5 and bmi < 25):
   print("Healthy")

elif ( bmi >= 25 and bmi < 30):
   print("overweight")

elif ( bmi >=30):
   print("severely overweight")

From: https://www.includehelp.com/python/bmi-body-mass-index-calculator.aspx

Does this answer your question?

Regards,

Will.

Upvotes: 0

Related Questions