首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算体重指数

计算体重指数
EN

Stack Overflow用户
提问于 2020-10-10 01:38:51
回答 5查看 1.5K关注 0票数 1

我正在写一个程序,它将提示用户输入6个想要计算其体重指数的个人的名字,然后提示每个人的身高和体重。然而,我似乎不知道如何从我创建的列表中选择一个人。它会一直选择所有输入的名字。有人能建议我如何一次只选择一个人吗?谢谢

代码语言:javascript
复制
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)
EN

回答 5

Stack Overflow用户

发布于 2020-10-10 01:47:39

在输入上循环六次,所有的列表都将工作,例如:

代码语言:javascript
复制
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)

编辑

以下是在列表中存储BMI的版本

代码语言:javascript
复制
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)
票数 3
EN

Stack Overflow用户

发布于 2020-10-10 01:44:26

试试这个吧

代码语言:javascript
复制
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'
票数 1
EN

Stack Overflow用户

发布于 2020-10-10 01:44:03

这样行得通吗?

Testrun:

python BMI.py

代码语言:javascript
复制
Enter height in meters: 1.75
Enter weight in kg: 64
Your BMI is: 20.897959183673468 and you are: Healthy

Python代码:

代码语言:javascript
复制
# 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")

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

这回答了你的问题吗?

致以敬意,

将要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64284821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档