Monday, February 11, 2019

Python: BMI calculation using dict of lists

bmi=dict()

while True:
   name=input('Enter a name:')
   bmi[name]=[float(input('Enter weight in kgs:')),float(input('Height in feet:'))*0.3048]    
   bmi[name].insert(0,(bmi[name][0]/(bmi[name][1]**2)))
   opt=input('Do you want to add more names(input y/n):')
   if opt in ('N','n'):
      break   print()

for k,v in bmi.items():
   print(f"The BMI of {k} is {v[0]} and falls under category of:",end=' ')
   if (0 < v[0] <15):
      print("Very serverely underweight")
   elif (15 <= v[0] <16):
      print("Severely underweight")
   elif (16 <= v[0] < 18.5):
      print("Underweight")
   elif (18.5 <= v[0] < 25):
      print("Normal (Healthy Weight)")
   elif (25 <= v[0] < 30):
      print("Overweight")
   elif (30 <= v[0] < 35):
      print("Obese Class I (Moderately obese)")
   elif (35 <= v[0] < 40):
      print("Obese Class II (Severely obese)")
   elif (40 <= v[0]):
      print("Obese Class III (Very Severely Obese)")
   else:
      print("Done")

print()
print('\t'*2,'*'*10,'SUMMARY','*'*10)
print()

print("Names with lower BMI:",[l for l in bmi.keys() if (bmi[l][0]<18.5)])
print("Names with Normal BMI:",[l for l in bmi.keys() if (18.5<=bmi[l][0]<=25)])
print("Names with higher BMI:",[l for l in bmi.keys() if (bmi[l][0]>25)])

print() 

print("The number of individuals with lower BMI :",len([l for l in bmi.keys() if (bmi[l][0]<18.5)]))
print("The number of individuals with normal BMI :",len([l for l in bmi.keys() if (18.5<=bmi[l][0]<=25)]))
print("The number of individuals with higher BMI :",len([l for l in bmi.keys() if (bmi[l][0]>25)]))

print() 

print("The lowest BMI is:",min([bmi[l][0] for l in bmi.keys()]))
print("The Average BMI is:",sum([bmi[l][0] for l in bmi.keys()])/len([l for l in bmi.keys()]))
print("The maximun BMI is:",max([bmi[l][0] for l in bmi.keys()]))




Example:

% python funcbmi.py

Enter a name:Allen
Enter weight in kgs:78
Height in feet:5.6
Do you want to add more names(input y/n):y
Enter a name:Helen
Enter weight in kgs:45
Height in feet:5.
Do you want to add more names(input y/n):y
Enter a name:Alice
Enter weight in kgs:85
Height in feet:5.8
Do you want to add more names(input y/n):y
Enter a name:Steve
Enter weight in kgs:40
Height in feet:4
Do you want to add more names(input y/n):n

The BMI of Allen is 26.772481266050967 and falls under category of: Overweight
The BMI of Helen is 19.375038750077497 and falls under category of: Normal (Healthy Weight)
The BMI of Alice is 27.19775224198354 and falls under category of: Overweight
The BMI of Steve is 26.909776041774304 and falls under category of: Overweight

                 ********** SUMMARY **********

Names with lower BMI: []
Names with Normal BMI: ['Helen']
Names with higher BMI: ['Allen', 'Alice', 'Steve']

The number of individuals with lower BMI : 0
The number of individuals with normal BMI : 1
The number of individuals with higher BMI : 3

The lowest BMI is: 19.375038750077497
The Average BMI is: 25.063762074971578
The maximun BMI is: 27.19775224198354