首页 > 解决方案 > 无法解压不可迭代的 int 对象。体重指数计算器

问题描述

我正在尝试打印出我的 BMI 计算器每个类别的人数。接收错误:> overweight, underweight, normal, obese = 0 TypeError: cannot unpack non-iterable int object

recipients = ["John", "Dee", "Aleister", "Lilith", "Paul", "Reggy"]
BMI_calc = []


def BMI(weights, heights):
    bmi_total = (weights * 703) / (heights ** 2)
    return bmi_total


def check(BMI):
  if BMI <= 18.5:
    print("Your underweight.")

  elif BMI > 18.5 and BMI < 24.9:
    print("You're normal weight.")

  elif BMI > 25 and BMI < 29.9:
    print("You're overweight.")

  elif BMI > 30:
    print("You're obese.")


for recipient in recipients:
    heights_ = int(input("What is your height " + recipient + "  :" ))
    weights_ = int(input("What is your weight " + recipient + "  :" ))
    BMI_info={"name":recipient,"weight":weights_,"height":heights_,"BMI":BMI(weights_, heights_)}
    BMI(BMI_info["weight"],BMI_info["height"])
    BMI_calc.append(BMI_info)



for person_info in BMI_calc:
    print(person_info["name"],end="\t")
    check(person_info["BMI"])


overweight, underweight, normal, obese = 0

def check(BMI):
  if BMI <= 18.5:
    print("Your underweight.")
    underweight += 1
  elif BMI > 18.5 and BMI < 24.9:
    print("You're normal weight.")
    normal += 1

  elif BMI > 25 and BMI < 29.9:
    print("You're overweight.")
    overweight +=1

  elif BMI > 30:
    print("You're obese.")
    obese += 1

print("This many people are" ,underweight)

我正在尝试打印出“这么多人体重不足,而对于超重、正常和肥胖的个体而言,这一类别的价值相同。

标签: python

解决方案


这个问题是你试图在一行中为 4 个不同的变量赋值,而 python 希望你给出一个包含 4 个不同值的元组,而不是一个;它不会将您的一个值广播到 4 个不同的变量。

您有 2 个选项,您可以在不同的行中将变量的值设置为 0,如下所示:

overweight = 0
underweight = 0
normal = 0
obese = 0

或者你可以在一行中完成,但不是只有一个 0,而是应该给出一个包含四个 0 的元组,如下所示:

overweight, underweight, normal, obese = (0, 0, 0, 0)

推荐阅读