我正在尝试为一个项目构建一个 BMI 计算器。它必须提示用户输入 6 个成员(数组中的元素)的身高和体重,计算输入并打印每个成员的 BMI。我已经处理了这部分,但我在使用它来填充第二个数组并计算体重不足、超重或正常体重的人数时遇到了麻烦。这是我到目前为止所拥有的:
Names = ["Jim", "Josh", "Ralph", "Amber", "Chris", "Lynn"]
Hs = []
Ws = []
UW = 18.5
OW = 25
for Name in Names:
Hs.append(int(input("What is your height in inches, " + Name + ": ")))
Ws.append(int(input("What is your weight in pounds, " + Name + ": ")))
def BMI(Ws, Hs):
for W, H in zip(Ws, Hs):
bmi_total = (W * 703) / (H ** 2)
for Name in Names:
print("The BMI for " + Name + " is: ", bmi_total)
BMI(Ws, Hs)
BMIScore = [bmi_total]
countUW = 0
countHW = 0
countOW = 0
for i in BMIScore:
if i <= UW:
countUW = countUW + 1
print("There are ", countUW, "underweight individuals.")
if i > UW and i < OW:
countHW = countHW + 1
print("There are ", countHW, "individuals with a healthy weight.")
if i >= OW:
countOW = countOW + 1
print("There are ", countOW, "overweight inividuals.")
这是我得到的输出:
What is your height in inches, Jim: 5
What is your weight in pounds, Jim: 5
What is your height in inches, Josh: 5
What is your weight in pounds, Josh: 5
What is your height in inches, Ralph: 5
What is your weight in pounds, Ralph: 5
What is your height in inches, Amber: 5
What is your weight in pounds, Amber: 5
What is your height in inches, Chris: 5
What is your weight in pounds, Chris: 5
What is your height in inches, Lynn: 5
What is your weight in pounds, Lynn: 5
The BMI for Jim is: 140.6
The BMI for Josh is: 140.6
The BMI for Ralph is: 140.6
The BMI for Amber is: 140.6
The BMI for Chris is: 140.6
The BMI for Lynn is: 140.6
Traceback (most recent call last):
File "C:\Users\Crase\AppData\Local\Programs\Python\Python39\Final Project.py", line 30, in <module>
if i <= UW:
TypeError: '<=' not supported between instances of 'tuple' and 'float'
此外,有没有办法在“print(”“+ Name +”的BMI是:“,bmi_total)”行中添加数组中的特定个体是超重还是体重不足?
任何援助将不胜感激。如果我的介绍有点草率,请原谅我。这是我第一次发帖。