![](/img/trans.png)
[英]BMI calculator in python: too many if statements how to reduce that?
[英]Bmi calculator if statements do not work in python
我一直在编写一个程序来计算 bmi 并对它们进行分类。
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)
if int(BMI)< 16:
print( user + "BMI is:" + str(bmi) + "and you are:severely underweight")
elif int(BMI)>= 16 and bmi < 18.5:
print( user + "BMI is:" + str(bmi) + "and you are:severely underweight")
elif int(BMI) >= 18.5 and bmi < 25:
print( user + "BMI is:" + str(bmi) + "and you are:severely Healthy")
elif int(BMI)>= 25 and bmi < 30:
print( user + "BMI is:" + str(bmi) + "and you are:severely overweight")
elif int(BMI) >=30:
print( user +" " + "BMI is:" + str(bmi) + "and you are:severely overweight")
if 语句不起作用,我不知道如何对超重、健康的人进行分类
必须进行相当多的更改才能使这项工作:
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 = []
messages = []
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(weight * 703 / height ** 2)
messages.append(user + ", your BMI is: " + str(BMIs[-1]))
for m in messages:
print(m)
for u in zip(individuals, BMIs):
if u[1] < 16:
print(u[0] + "BMI is:" + str(u[1]) + " and you are:severely underweight")
elif 16 <= u[1] < 18.5:
print(u[0] + "BMI is:" + str(u[1]) + " and you are:severely underweight")
elif 18.5 <= u[1] < 25:
print(u[0] + "BMI is:" + str(u[1]) + " and you are:severely Healthy")
elif 25 <= u[1] < 30:
print(u[0] + "BMI is:" + str(u[1]) + " and you are:severely overweight")
elif u[1] >= 30:
print(u[0] + " " + "BMI is:" + str(u[1]) + " and you are:severely overweight")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.