Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import statistics
  2. user_stats = []
  3.  
  4.  
  5. def program_menu():
  6. print("Welcome message to the user to explain what the programm is going to do")
  7. choice = continue_choice()
  8. if choice.lower() == 'y':
  9. stats = collect_user_health_stats('y')
  10. print("there are ", len(stats), "in the data set.")
  11. print("The values as follows:", end=" ")
  12. print(stats)
  13. get_lowest(stats, 'age')
  14. get_highest(stats, 'age')
  15. get_average(stats, 'age')
  16. get_mean(stats, 'age')
  17. # get_mode(stats, 'age')
  18. get_range(stats, 'age')
  19. get_lowest(stats, 'weight')
  20. get_highest(stats, 'weight')
  21. get_average(stats, 'weight')
  22. get_mean(stats, 'weight')
  23. #get_mode(stats, 'weight')
  24. get_range(stats, 'weight')
  25. get_lowest(stats, 'height')
  26. get_highest(stats, 'height')
  27. get_average(stats, 'height')
  28. get_mean(stats, 'height')
  29. #get_mode(stats, 'height')
  30. get_range(stats, 'height')
  31. get_lowest(stats, 'bmi')
  32. get_highest(stats, 'bmi')
  33. get_average(stats, 'bmi')
  34. get_mean(stats, 'bmi')
  35. #get_mode(stats, 'bmi')
  36. get_range(stats, 'bmi')
  37.  
  38.  
  39. else:
  40. return
  41.  
  42.  
  43. def continue_choice():
  44. print("Would you like to continue? (Y/N)", end=" ")
  45. choice = str(input()).upper()
  46. print(choice)
  47. while choice != "Y" and choice != "N":
  48. print("invalid option:Would you like to continue ?(Y/N) . ")
  49. choice = str(input()).upper()
  50. return choice
  51.  
  52. def collect_user_health_stats(choice):
  53. while choice == 'y':
  54. height = get_user_height()
  55. weight = get_user_weight()
  56. age = get_user_age()
  57. bmi = weight / height
  58. bmi = round((bmi / height),2)
  59. weight_category = weight_category(bmi)
  60.  
  61. user_stats.append({'height': height, 'weight': weight, 'age': age, 'bmi': bmi, 'weight_category': weight_category})
  62. print("Do you want to enter stats for another person? (Y/N)", end=" ")
  63. choice = continue_choice()
  64. print(choice)
  65. collect_user_health_stats(choice.lower())
  66. return user_stats
  67.  
  68. def get_user_height():
  69. height = get_height_input()
  70. param = get_height_measured_input()
  71. height = calculate_height(float(height), str(param))
  72. return height
  73.  
  74.  
  75. def get_height_input():
  76. print("Please enter person height!", end=" ")
  77. height = eval(input())
  78. if not height or type(height) not in (float, int):
  79. print("Invalid height entered!")
  80. get_height_input()
  81. return height
  82.  
  83.  
  84. def get_height_measured_input():
  85. print("Height measured in meters(m) or feet (ft) or inches (in) ?", end=" ")
  86. param = input()
  87. if not param or type(param) != str or str(param) not in ('m', 'meters', 'ft', 'feet', 'in', 'inches',):
  88. print("Invalid choice selected")
  89. get_height_measured_input()
  90. return param
  91.  
  92.  
  93. def calculate_height(height, param):
  94. if param == 'ft' or param == 'feet':
  95. return height / 3.2808 # 1 meter = 3.2808 feet
  96. elif param == 'in' or param == 'inches':
  97. return height / 39.370 # 1 meter = 39.3701 inch
  98. else:
  99. return height
  100.  
  101.  
  102. def get_user_weight():
  103. weight = get_weight_input()
  104. param = get_weight_measured_input()
  105. weight = calculate_weight(float(weight), str(param))
  106. return weight
  107.  
  108.  
  109. def get_weight_input():
  110. print("Please enter person weight!", end=" ")
  111. weight = eval(input())
  112. if not weight or type(weight) not in (float, int):
  113. print("Invalid weight entered!")
  114. get_weight_input()
  115. return weight
  116.  
  117.  
  118. def get_weight_measured_input():
  119. print("Weight measured in pounds(lb) or stone (st) or kilograms (kg) ?", end=" ")
  120. param = input()
  121. if not param or type(param) != str or str(param) not in ('lb', 'pounds', 'st', 'stone', 'kg', 'kilograms',):
  122. print("Invalid choice selected")
  123. get_weight_measured_input()
  124. return param
  125.  
  126.  
  127. def calculate_weight(weight, param):
  128. if param == 'lb' or param == 'pounds':
  129. return weight / 2.2046 # 1 kg = 2.2046 pounds
  130. elif param == 'st' or param == 'stone':
  131. return weight / 0.15747 # 1 kg = 0.15747 stone
  132. else:
  133. return weight
  134.  
  135.  
  136. def get_user_age():
  137. print("Please enter person age", end=" ")
  138. age = eval(input())
  139. if not age or type(age) not in (float, int):
  140. print("Invalid age entered!")
  141. get_user_age()
  142. return int(age)
  143.  
  144. def weight_category(bmi):
  145.  
  146.  
  147. if bmi <= 18.5:
  148. print(" You are underweight.")
  149. elif bmi > 18.5 and bmi < 25:
  150. print(" You are Healty.")
  151.  
  152. elif bmi > 25 and bmi < 30:
  153. print("You are overweight.")
  154.  
  155. elif bmi > 30:
  156. print("You are obese.")
  157.  
  158. else:
  159.  
  160. print(" Ther is an error please try again")
  161. return weight_category
  162.  
  163. def spread_stats(stats, key):
  164. return [x[key] for x in stats]
  165.  
  166.  
  167. def get_average(stats, key):
  168. # this function return average from list
  169. sum_total = sum(spread_stats(stats, key))
  170. avg = sum_total / len(spread_stats(stats, key))
  171. print("The average {key} in the list is :".format(key=key), avg)
  172. return round((avg),2)
  173.  
  174.  
  175. def get_lowest(stats, key):
  176. # this function return max from list
  177. value = min(spread_stats(stats, key))
  178. print("The lowest {key} entered is :".format(key=key), value)
  179. return value
  180.  
  181.  
  182. def get_highest(stats, key):
  183. # this function return max from list
  184. value = max(spread_stats(stats, key))
  185. print("The highest {key} entered is :".format(key=key), value)
  186. return value
  187.  
  188.  
  189. def get_mean(stats,key):
  190. numbers = spread_stats(stats, key)
  191. value = float(sum(numbers)) / max(len(numbers), 1)
  192. print("The mean {key} entered is :".format(key=key), value)
  193. return round((value),2)
  194.  
  195.  
  196. def get_mode(stats,key):
  197. numbers = spread_stats(stats, key)
  198. value = statistics.mode(numbers)
  199. print("The mode {key} entered is :".format(key=key), value)
  200. return round((value),2)
  201.  
  202.  
  203. def get_range(stats,key):
  204. numbers = spread_stats(stats, key)
  205. range = max(numbers) - min(numbers)
  206. print("The range {key} entered is : {minimum} - {maximum}".format(key=key,minimum = min(numbers), maximum = max(numbers)))
  207. return range
  208. program_menu()
Add Comment
Please, Sign In to add comment