python - 无法解压不可迭代的 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)
我正在尝试打印出“这么多人体重不足,而对于超重、正常和肥胖的个体而言,这一类别的价值相同。
解决方案
这个问题是你试图在一行中为 4 个不同的变量赋值,而 python 希望你给出一个包含 4 个不同值的元组,而不是一个;它不会将您的一个值广播到 4 个不同的变量。
您有 2 个选项,您可以在不同的行中将变量的值设置为 0,如下所示:
overweight = 0
underweight = 0
normal = 0
obese = 0
或者你可以在一行中完成,但不是只有一个 0,而是应该给出一个包含四个 0 的元组,如下所示:
overweight, underweight, normal, obese = (0, 0, 0, 0)
推荐阅读
- c++ - 为什么会这样编译?cout<"是";
- sql - 基于日期和 MAX 值的查询,非常感谢帮助
- java - 使用 Java 更改 XML 中输入的值
- ios - 为什么安装应用程序提示无法安装请稍后在 iOS 13 中重试
- android - 接收广播 Intent 时出错 act=com.android.vending.billing.PURCHASES_UPDATED
- python-3.x - or-tools 在这种情况下如何使用 OnlyEnforceIf
- python - 如何在 python 中获取 xgb.train 的超参数
- c# - 为航点位置和链接创建 json
- python - 如何停止 Python 中的所有线程?
- flutter - 是否可以在 Flutter 中的图像文件上绘制文本?