![](/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.