Reputation: 33
I am trying to print out the number of people in each category of my BMI calculator. Receiving error: > 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)
I am trying to print out "This many people are underweight with the value of individuals it that category same for over, normal and obese.
Upvotes: 0
Views: 116
Reputation: 11741
Your error tells you what is going wrong. It's on the following line
overweight, underweight, normal, obese = 0
What that line tries to do is "unpack" (or separate) out the right side of the equal sign and put it into each of the four variables on the left. 0
cannot be broken part, it's not a tuple
If you want to initialize each variable to 0
simply do each on a different line.
Or you could assign them to a tuple that it's separated out
overweight, underweight, normal, obese = (0, 0, 0, 0)
But this seems like overkill and less readable IMO
UPDATE
Per comment below, since ints are immutable you can do the following
overweight = underweight = normal = obese = 0
Upvotes: 1
Reputation: 816
This problem is you're trying to assign a value to 4 different variables in one line, and python expects you to give a tuple containing 4 different values, not one; it doesn't broadcast your one value to 4 different variables.
You have 2 option, you can set the value of your variables to 0 in a different line like this:
overweight = 0
underweight = 0
normal = 0
obese = 0
Or you can do it in one line, but instead of only one 0, you should give a tuple containing four 0, like this:
overweight, underweight, normal, obese = (0, 0, 0, 0)
Upvotes: 1