0

我正在尝试为一个项目构建一个 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)”行中添加数组中的特定个体是超重还是体重不足?

任何援助将不胜感激。如果我的介绍有点草率,请原谅我。这是我第一次发帖。

4

3 回答 3

1

如果你不能使用面向对象的方法,你可以使用元组列表来表示一个人:

names = ["Jim", "Josh", "Ralph", "Amber", "Chris", "Lynn"]
people = []

for name in names:
    height = int(input("What is your height in inches, " + name + ": "))
    weight = int(input("What is your weight in pounds, " + name + ": "))

    # Calculate the BMI while iterating over the list of names
    bmi = (weight * 703) / (height ** 2)

    people.append((name, height, weight, bmi))

然后,您可以使用字典来存储 overs、healthies 和 unders:

UW_LIM = 18.5
OW_LIM = 25

stats = {"OW": 0, "HW": 0, "UW": 0}
for name, _, _, bmi in people:
    print("The BMI for", name, "is", bmi)
    key = "UW" if (bmi < UW_LIM) else "HW" if (UW_LIM < bmi < OW_LIM) else "OW"
    stats[key] += 1

最后,只需将它们打印出来:

print("There are", stats["UW"], "underweight individuals.")
print("There are", stats["HW"], "individuals with a healthy weight.")
print("There are", stats["OW"], "overweight inividuals.")

为了好玩,这里有一个 OOP 方法:

class Person:
    def __init__(self, name: str, height: float, weight: float):
        self.name = name
        self.height = height
        self.weight = weight

    @property
    def bmi(self):
        return (self.weight * 703) / (self.height ** 2)

    @property
    def classification(self):
        return 0 if (bmi < UW_LIM) else 1 if (UW_LIM < bmi < OW_LIM) else 2


names = ["Jim", "Josh", "Ralph", "Amber", "Chris", "Lynn"]

#        UW  HW  OW
stats = [ 0,  0,  0]

for name in names:
    height = int(input("What is your height in inches, " + name + ": "))
    weight = int(input("What is your weight in pounds, " + name + ": "))
    person = Person(name, height, weight)
    
    print("The BMI for", person.name, "is", person.bmi)

    stats[person.classification] += 1

print("There are", stats[0], "underweight individuals.")
print("There are", stats[1], "individuals with a healthy weight.")
print("There are", stats[2], "overweight inividuals.")
于 2021-07-12T00:23:23.510 回答
0

回答第一个问题,您可能想尝试更改ifloat(i). 我不确定这是否适用于所有循环,但您需要定义变量“i”的类型。为了回答第二个问题,您能否提供有关“超重”和“体重不足”的更多信息,但您可以添加如下声明:

if bmi_total <= 150:
    #etc.
于 2021-07-12T00:20:44.947 回答
0

您的 bmi_total 在您的 BMI 函数中定义,但随后在其外部使用。我在尝试您的代码时遇到的错误特别指出了这一点。为了解决这个问题,您可以将 bmi_total 设为全局,以便可以在函数外部访问它,如下所示:

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):
    # changed bmi_total to be a global variable here
    global bmi_total
    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.")

但是,最好让 BMI 函数计算 BMI 并将其返回。大多数程序员都直接或间接地拥护 KISS(保持愚蠢)原则。基本上,将每个功能块分成一个逻辑函数(如 BMI())并让它只做一件事和一件事(在这种情况下,计算 BMI)。

正如其他人所说,您可能还希望将每个人的姓名、体重、身高和 BMI 分组到一个容器中,以使事情更有条理。这可以是列表、字典、类或任何数量的其他将相关信息保持在一起的方式的列表。好的开始!

于 2021-07-12T01:58:10.493 回答