fork download
  1. print("Lets calculate your BMI!\n")
  2.  
  3. valid_region = False
  4. while valid_region == False:
  5. #prompts for user's region/race
  6. user_region = input("Enter ‘1’ for US/Europe or ‘2’ for Asian as the \
  7. selected region/race. Please enter the numeric value only without any leading \
  8. or trailing spaces.: ")
  9. if user_region == '1' or user_region == '2':
  10. valid_region = True
  11. #prompts for user's preferred system of measurement
  12. valid_system = False
  13. while valid_system == False:
  14. preferred_system = input("\nEnter ‘1’ for Metric (i.e. kg & m) or \
  15. ‘2’ for Imperial (i.e. lbs & inches) as the preferred measurement system. \
  16. Please enter the numeric value only without any leading or trailing spaces.: ")
  17. if preferred_system == '1' or preferred_system == '2':
  18. valid_system = True
  19. #if Metric is chosen
  20. if preferred_system == '1':
  21. valid_weight = False
  22. while valid_weight == False:
  23. #prompts for Metric weight in kilograms
  24. user_weight = input("\nEnter your weight in kilograms \
  25. (kg). Please enter a numeric value only. Decimals are allowed. Do not \
  26. include any leading or trailing spaces. (e.g. '53.5'): ")
  27. try:
  28. weight_float = float(user_weight)
  29. if weight_float > 0:
  30. valid_weight = True
  31. valid_height = False
  32. while valid_height == False:
  33. #prompts for Metric height in meters
  34. user_height = input("\nEnter your height \
  35. in meters (m). Please enter a numeric value only. Decimals are allowed. Do \
  36. not include any leading or trailing spaces. (e.g. '1.7'): ")
  37. try:
  38. height_float = float(user_height)
  39. if height_float > 0:
  40. valid_height = True
  41. #calculate BMI using Metric formula
  42. metric_bmi = weight_float / \
  43. (height_float ** 2)
  44. if user_region == '1':
  45. if metric_bmi < 18.5:
  46. category = "underweight"
  47. elif 18.5 <= metric_bmi < 25:
  48. category = "normal"
  49. elif 25 <= metric_bmi < 30:
  50. category = "overweight"
  51. else:
  52. category = "obese"
  53. elif user_region == '2':
  54. if metric_bmi < 18.5:
  55. category = "underweight"
  56. elif 18.5 <= metric_bmi < 23:
  57. category = "normal"
  58. elif 23 <= metric_bmi < 25:
  59. category = "overweight"
  60. else:
  61. category = "obese"
  62. print("\nYour calculated BMI is \
  63. {:.2f} kg/m^2. Based on the result, you are categorized as \
  64. '{}'.".format(metric_bmi, category))
  65. except ValueError:
  66. print("\nInvalid input for height. \
  67. Please try again!\n")
  68. except ValueError:
  69. print("\nInvalid input for weight. Please try \
  70. again!\n")
  71. #if Imperial is chosen
  72. if preferred_system == '2':
  73. valid_weight = False
  74. while valid_weight == False:
  75. #prompts for Imperial weight in pounds
  76. user_weight = input("\nEnter your weight in pounds \
  77. (lbs). Please enter a numeric value only. Decimals are allowed. Do not \
  78. include any leading or trailing spaces. (e.g. '118'): ")
  79. try:
  80. weight_float = float(user_weight)
  81. if weight_float > 0:
  82. valid_weight = True
  83. valid_height = False
  84. while valid_height == False:
  85. #prompts for Imperial height in inches
  86. user_height = input("\nEnter your height \
  87. in inches (in). Please enter a numeric value only. Decimals are allowed. Do \
  88. not include any leading or trailing spaces. (e.g. '67'): ")
  89. try:
  90. height_float = float(user_height)
  91. if height_float > 0:
  92. valid_height = True
  93. #calculate BMI - Imperial formula
  94. imperial_bmi = \
  95. (weight_float * 703) / \
  96. (height_float ** 2)
  97. #convert weight & height to Metric
  98. weight_kg = weight_float * 0.453592
  99. height_m = height_float * 0.0254
  100. #calculate BMI - Metric formula
  101. metric_bmi = weight_kg / \
  102. (height_m ** 2)
  103. if user_region == '1':
  104. if metric_bmi < 18.5:
  105. category = "underweight"
  106. elif 18.5 <= metric_bmi < 25:
  107. category = "normal"
  108. elif 25 <= metric_bmi < 30:
  109. category = "overweight"
  110. else:
  111. category = "obese"
  112. elif user_region == '2':
  113. if metric_bmi < 18.5:
  114. category = "underweight"
  115. elif 18.5 <= metric_bmi < 23:
  116. category = "normal"
  117. elif 23 <= metric_bmi < 25:
  118. category = "overweight"
  119. else:
  120. category = "obese"
  121. print("\nYour calculated BMI is \
  122. {:.2f} lb/in^2. Based on the result, you are categorized as \
  123. '{}'.".format(imperial_bmi, category))
  124. except ValueError:
  125. print("\nInvalid input for height. \
  126. Please try again!\n")
  127. except ValueError:
  128. print("\nInvalid input for weight. Please try \
  129. again!\n")
  130. else:
  131. print("\nInvalid input for preferred system of measurement. \
  132. Please try again!\n")
  133. else:
  134. print("\nInvalid input for region/race. Please try again!\n")
Success #stdin #stdout 0.04s 9868KB
stdin
1
2
118
67
stdout
Lets calculate your BMI!

Enter ‘1’ for US/Europe or ‘2’ for Asian as the selected region/race. Please enter the numeric value only without any leading or trailing spaces.: 
Enter ‘1’ for Metric (i.e. kg & m) or ‘2’ for Imperial (i.e. lbs & inches) as the preferred measurement system. Please enter the numeric value only without any leading or trailing spaces.: 
Enter your weight in pounds (lbs). Please enter a numeric value only. Decimals are allowed. Do not include any leading or trailing spaces. (e.g. '118'): 
Enter your height in inches (in). Please enter a numeric value only. Decimals are allowed. Do not include any leading or trailing spaces. (e.g. '67'): 
Your calculated BMI is 18.48 lb/in^2. Based on the result, you are categorized as 'underweight'.