无法解压缩不可iterable int对象。体重指数计算器

2024-09-28 21:55:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图打印出我的体重指数计算器中每个类别的人数。接收错误:>; 超重、体重不足、正常、肥胖=0 TypeError:无法解压缩不可编辑的int对象

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)

我试着打印出“许多人体重不足,与个人价值相比,过度、正常和肥胖属于同一类别


Tags: andreinfoyouprintbminormalweight
2条回答

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

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

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

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

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

你的错误告诉你哪里出了问题。在下一行

overweight, underweight, normal, obese = 0

该行试图做的是“解包”(或分离)出等号的右侧,并将其放入左侧的四个变量中0不能被分解,它不是元组

如果要将每个变量初始化为0,只需在不同的行上执行每个操作

或者你可以把它们分配给一个分开的元组

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

但在我看来,这似乎是矫枉过正,可读性较差

更新

根据下面的注释,由于int是不可变的,因此可以执行以下操作

overweight = underweight = normal = obese = 0

相关问题 更多 >