Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. def validate_is_positive_numeric(val):
  2. try:
  3. val = float(val)
  4. if val <= 0:
  5. print("Enter a valid positive value")
  6. return None, False
  7. except ValueError:
  8. print("Enter a valid numeric value")
  9. return None, False
  10.  
  11. return val, True
  12.  
  13.  
  14. def read_input(text):
  15. value, success = validate_is_positive_numeric(input(text))
  16. if not success:
  17. read_input(text=text)
  18.  
  19. return value
  20.  
  21.  
  22. def calculate_average(macro_name, total_quantity, total_patients):
  23. avg = total_quantity/int(total_patients)
  24. print("Amount of {} (g) required : {}".format(macro_name, avg))
  25.  
  26.  
  27. num_patients = read_input("Enter the number of patients: ")
  28.  
  29.  
  30. protein, fats, carbs, kilojoules = 0, 0, 0, 0
  31. for _ in range(int(num_patients)):
  32. protein += read_input("Amount of protein (g) required: ")
  33. fats += read_input("Amount of fats (g) required: ")
  34. carbs += read_input("Amount of carbohydrates (g) required: ")
  35. kilojoules = 4.18*(4*protein + 4*carbs + 9.30*fats)
  36.  
  37. calculate_average(macro_name="Protein", total_quantity=protein, total_patients=num_patients)
  38. calculate_average(macro_name="Fats", total_quantity=fats, total_patients=num_patients)
  39. calculate_average(macro_name="Carbohydrates", total_quantity=carbs, total_patients=num_patients)
  40. calculate_average(macro_name="Kilojoules", total_quantity=kilojoules, total_patients=num_patients)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement