135k views
2 votes
Tracking Fat Burning Heart Rate and BMI with File Input and Output

create a file that helps to track the calculated fat burning heart rate and bmi over time. The file will either need to be created or open an existing file. The user could also just select to read the result from the file.

SAMPLE:

Welcome to the Weber State University Human Performance Lab!
Please utilize the following calculator to find your ideal fat burning heart rate and BMI.
The program will also store this information in a file you choose so that it can be tracked over time.

Select:
1. Create a file and add results
2. Open a file and add results
3. Read results from file
4. Quit
1

Enter the name of the file you would like to create:
BMICalculations.txt

Enter age: forty
Invalid age. Must be a number.
Re-enter age: 40
Enter height in inches: hi
Invalid inches value. Must be a number.
Re-enter height in inches: 0
Invalid inches value. Must be positive.
Re-enter height in inches: 69.25
Enter weight in pounds: dog
Invalid pounds value. Must be a number.
Re-enter weight in pounds: -3
Invalid pounds value. Must be a number.
Re-enter weight in pounds: -20
Invalid pounds value. Must be positive.
Re-enter weight in pounds: 150.5

Age = 40
Height = 69.25"
Weight = 150.5 pounds

Fat Burning Heart Rate = 126 bpm
Body Mass Index = 22.1

Results added to file.

Select:
1. Create a file and add results
2. Open a file and add results
3. Read results from file
4. Quit
3

FBHR: 126bpm - BMI 22.1
MY CODE SO FAR :

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.\\")

check = True
ageText = "Enter age:"
while check:
check = False
try:
age = int(input(ageText))
if age <= 0:
check = True
print("Invalid age value. Must be positive.")
except ValueError:
check = True
print("Invalid age. Must be a number.")
if check==True:
ageText = "Re-Enter age:"



check = True
heightText = "Enter height in inches:"
while check:
check = False
try:
height = float(input(heightText))
if height <= 0:
check = True
print("Invalid inches value. Must be positive.")
except ValueError:
check = True
print("Invalid inches value. Must be a number.")
if check==True:
heightText = "Re-Enter height in inches:"


check = True
weightText = "Enter weight in pounds:"
while check:
check = False
try:
weight = float(input(weightText))
if weight <= 0:
check = True
print("Invalid pounds value. Must be positive.")
except ValueError:
check = True
print("Invalid pounds value. Must be a number.")
if check==True:
weightText = "Re-Enter weight in pounds:"



print()
print("Age:",age)
height_with_unit = str(height)+'"'
print("Height:",height_with_unit)
print("Weight:",weight,"pounds")
print()


max_heart_rate = 220-age
fat_burning_heart_rate=round(0.7*max_heart_rate)

print("Fat Burning Heart Rate =",fat_burning_heart_rate,"bpm")

#calculate and print body mass index
bmi = round((weight/(height*height))*703,1)
print("Body Mass Index =",bmi)

User Jambaaz
by
7.7k points

1 Answer

0 votes

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.

User Silentnuke
by
7.9k points

No related questions found