【发布时间】:2021-03-25 18:43:40
【问题描述】:
在我的代码中,我尝试使用第二个循环,它应该遍历体重指数数组并调用另一个函数,该函数接受体重指数作为参数并返回个人是否体重过轻、正常体重或超重。
这就是我卡住的地方。代码.....
individuals = ["Mike Jackson", "Mike Tyson", "Mike Jordan"]
bodymass = list()
index = 0
def BMI(height, weight):
bmi = (weight * 703) / (height ** 2)
return bmi
for individual in individuals:
print("Do you want to know the BMI for", individual, "?")
print("The weight is required for", individual)
weight = eval(input("Enter weight in pounds here: "))
print("The height is required for", individual)
height = eval(input("Enter height in inches for here: "))
bmi = BMI(height, weight)
bodymass.append(bmi)
print("The body mass index for", individual, "is: ", (bmi))
for i in range(len(bodymass)):
if 18.5 > bodymass[i]:
print("You are underweight.")
elif 18.5 <= bodymass[i] < 25:
print("Your weight is normal.")
elif bodymass[i] >= 25:
print("You are overweight.")
==============================================
这是结果...
Do you want to know the BMI for Mike Jackson ?
The weight is required for Mike Jackson
Enter weight in pounds here: 175
The height is required for Mike Jackson
Enter height in inches for here: 72
The body mass index for Mike Jackson is: 23.73167438271605
Your weight is normal.
Do you want to know the BMI for Mike Tyson ?
The weight is required for Mike Tyson
Enter weight in pounds here: 184
The height is required for Mike Tyson
Enter height in inches for here: 80
The body mass index for Mike Tyson is: 20.21125
Your weight is normal.
Your weight is normal.
Do you want to know the BMI for Mike Jordan ?
The weight is required for Mike Jordan
Enter weight in pounds here: 215
The height is required for Mike Jordan
Enter height in inches for here: 78
The body mass index for Mike Jordan is: 24.843030900723207
Your weight is normal.
Your weight is normal.
Your weight is normal.
Process finished with exit code 0
=====================================
我遇到的问题是下一个输入值的人或对象,前一个人的结果显示。因此,第三人称显示“您体重不足”的 3 个值,而不是仅显示该人的结果。
您体重过轻。 你体重过轻。 你体重过轻。
【问题讨论】:
标签: python arrays python-3.x list numpy