Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def validate_is_positive_numeric(val):
- try:
- val = float(val)
- if val <= 0:
- print("Enter a valid positive value")
- return None, False
- except ValueError:
- print("Enter a valid numeric value")
- return None, False
- return val, True
- def read_input(text):
- value, success = validate_is_positive_numeric(input(text))
- if not success:
- read_input(text=text)
- return value
- def calculate_average(macro_name, total_quantity, total_patients):
- avg = total_quantity/int(total_patients)
- print("Amount of {} (g) required : {}".format(macro_name, avg))
- num_patients = read_input("Enter the number of patients: ")
- protein, fats, carbs, kilojoules = 0, 0, 0, 0
- for _ in range(int(num_patients)):
- protein += read_input("Amount of protein (g) required: ")
- fats += read_input("Amount of fats (g) required: ")
- carbs += read_input("Amount of carbohydrates (g) required: ")
- kilojoules = 4.18*(4*protein + 4*carbs + 9.30*fats)
- calculate_average(macro_name="Protein", total_quantity=protein, total_patients=num_patients)
- calculate_average(macro_name="Fats", total_quantity=fats, total_patients=num_patients)
- calculate_average(macro_name="Carbohydrates", total_quantity=carbs, total_patients=num_patients)
- calculate_average(macro_name="Kilojoules", total_quantity=kilojoules, total_patients=num_patients)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement