Mussab_Blue

BMI calculator

Jul 16th, 2022 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. bmi_categories = {0:"Severely Underweight",
  2.                   1:"Underweight",
  3.                   2:"Normal",
  4.                   3:"Overweight",
  5.                   4:"Moderately Obese",
  6.                   5:"Severely Obese",
  7.                   6:"Morbidly Obese"}
  8. STD_BMI_VALUES = (15, 18, 18.5, 25, 30, 35, 40, 60)
  9.  
  10. def get_bmi_category(bmi):
  11.     for i in range(len(STD_BMI_VALUES)-1):
  12.         mn, mx = STD_BMI_VALUES[i], STD_BMI_VALUES[i+1]
  13.         if (bmi >= mn and bmi < mx): bmi = i
  14.     return bmi_categories.get(bmi, "Uncategorized")
  15.  
  16. std = {"Height":(1.4, 2, 'meters'),"Weight":(35, 120, 'Kg')}
  17.  
  18. def get_user_input(_type, n='E'):
  19.     mn, mx, unit = std[_type]
  20.     try:
  21.         _input = float(input(f"\n{n}nter your {_type} in {unit}: "))
  22.         if not (_input >= mn and _input <= mx): raise Warning()
  23.         return _input
  24.     except Exception as e:
  25.         print(("Invalid Input.", f"{_type} out of acceptable range.")[isinstance(e, Warning)]) 
  26.     return get_user_input(_type, 'Please re-e')
  27.  
  28. weight, height = get_user_input('Weight'), get_user_input('Height')
  29. rating = get_bmi_category(round(weight/height**2, 1))
  30. print(f"Based on your weight of {weight} Kg, and your height of {height} meters, your BMI rating is {rating}")
  31.  
Add Comment
Please, Sign In to add comment