To track the calculated fat burning heart rate and BMI over time, you can use the following code:
```python
def create_file(file_name):
try:
with open(file_name, 'w') as file:
print("File created successfully.")
except:
print("An error occurred while creating the file.")
def add_results(file_name, age, height, weight, fat_burning_heart_rate, bmi):
try:
with open(file_name, 'a') as file:
file.write(f"Age: {age}\\")
file.write(f"Height: {height}\"\\")
file.write(f"Weight: {weight} pounds\\")
file.write(f"Fat Burning Heart Rate: {fat_burning_heart_rate} bpm\\")
file.write(f"Body Mass Index: {bmi}\\\\")
print("Results added to file.")
except:
print("An error occurred while adding the results.")
def read_results(file_name):
try:
with open(file_name, 'r') as file:
print("Results from file:")
print(file.read())
except:
print("An error occurred while reading the file.")
def calculate_heart_rate(age):
max_heart_rate = 220 - age
fat_burning_heart_rate = round(0.7 * max_heart_rate)
return fat_burning_heart_rate
def calculate_bmi(height, weight):
bmi = round((weight / (height * height)) * 703, 1)
return bmi
def main():
print("Welcome to the Weber State University Human Performance Lab!")
print("Please utilize the following calculator to find your ideal fat burning heart rate and BMI.")
print("The program will also store this information in a file you choose so that it can be tracked over time.\\")
while True:
print("Select:")
print("1. Create a file and add results")
print("2. Open a file and add results")
print("3. Read results from file")
print("4. Quit")
choice = input("Enter your choice (1-4): ")
if choice == "1":
file_name = input("Enter the name of the file you would like to create: ")
age = get_positive_integer("Enter age: ")
height = get_positive_float("Enter height in inches: ")
weight = get_positive_float("Enter weight in pounds: ")
fat_burning_heart_rate = calculate_heart_rate(age)
bmi = calculate_bmi(height, weight)
add_results(file_name, age, height, weight, fat_burning_heart_rate, bmi)
elif choice == "2":
file_name = input("Enter the name of the file you would like to open: ")
age = get_positive_integer("Enter age: ")
height = get_positive_float("Enter height in inches: ")
weight = get_positive_float("Enter weight in pounds: ")
fat_burning_heart_rate = calculate_heart_rate(age)
bmi = calculate_bmi(height, weight)
add_results(file_name, age, height, weight, fat_burning_heart_rate, bmi)
elif choice == "3":
file_name = input("Enter the name of the file you would like to read: ")
read_results(file_name)
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice. Try again.")
def get_positive_integer(prompt):
while True:
try:
value = int(input(prompt))
if value <= 0:
print("Invalid value. Must be a positive integer.")
else:
return value
except ValueError:
print("Invalid input. Must be an integer.")
def get_positive_float(prompt):
while True:
try:
value = float(input(prompt))
if value <= 0:
print("Invalid value. Must be a positive float.")
else:
return value
except ValueError:
print("Invalid input. Must be a float.")
if __name__ == '__main__':
main()
```
This code provides a menu for the user to select different options:
1. Create a file and add results
2. Open a file and add results
3. Read results from file
4. Quit
The program prompts the user to enter the necessary information (age, height, and weight) and calculates the fat burning heart rate and BMI. It then either creates a new file or opens an existing file to store the results. The file is formatted with each person's information on separate lines and a blank line between individuals.
For input validation, the `get_positive_integer` function is used to ensure the entered value is a positive integer, and the `get_positive_float` function is used to ensure the entered value is a positive float.
Note: Make sure to handle any potential exceptions or errors related to file operations.