Advertisement
acclivity

pyBMI-Calculator

Jul 16th, 2022
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. def get_float(prompt):
  2.     while True:
  3.         s = input("\n" + prompt)
  4.         try:
  5.             return s, float(s)      # return the original string AND the float value
  6.         except:
  7.             print("Invalid input. Please re-enter")
  8.  
  9. while True:
  10.     height_str, height_float = get_float("Enter your height in metres: ")
  11.     if 1.35 < height_float < 2.1:
  12.         break
  13.     print("Height out of acceptable range. Please re-enter")
  14.  
  15. while True:
  16.     weight_str, weight_float = get_float("Enter your weight in Kg: ")
  17.     if 35 < weight_float < 120:
  18.         break
  19.     print("Weight out of acceptable range. Please re-enter")
  20.  
  21. bmi = weight_float / (height_float ** 2)
  22. cats = [-1, "severely underweight", 15, "underweight", 18.5, "normal", 25, "overweight",
  23.         30, "moderately obese", 35, "severely obese", 40, "morbidly obese"]
  24.  
  25. for x in range(0, len(cats), 2):
  26.     if bmi >= cats[x]:
  27.         text = cats[x+1]
  28. print(f"\nBased on your weight of {weight_str} Kg, and your height of {height_str} metres, your BMI rating is {text}")
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement