Question

I need to complete three different loops

I'm doing a fat burning heart rate and BMI calculator. It needs to account for incorrect inputs and loop back to request the correct input then move on to the next request for input.


    #Calculate ideal fat burning heart rate and BMI
    #(220 - age) * .7 for fat burning heart rate
    #BMI = 703 * lbs / height**2 (in)
    
    print("Welcome to the Weber State University Performance Lab!")
    print("Please utilize the following calculator to find your ideal fat burning heart rate and BMI.")
    
    #Age entry
    while True:
        try: 
            age = float(input("Please enter your age: "))
            if age <= 0:
                raise TypeError ("Please enter a number greater than zero.")
            break
        except ValueError:
                print("Invalid input.")
                print("Please enter your age in numeric format.")
        except TypeError as err:
                print(err)
        except:
                print("Invalid input.")
    while True:
        #Height entry
        try: 
            height = float(input("Please enter your height in inches: "))
            if height <= 0:
                raise TypeError ("Please enter a number greater than zero.")
            break
        except ValueError:
                print("Invalid input.")
                print("Please enter your height in inches in numeric format.")
        except TypeError as err:
                print(err)
        except:
                print("Invalid input.")
    
    while True:
        #Weight Entry
        try: 
            weight = float(input("Please enter your weight in pounds: "))
            if weight <= 0:
                raise TypeError ("Please enter a number greater than zero.")
            break
        except ValueError:
                print("Invalid input.")
                print("Please enter your weight in pounds in numeric format.")
        except TypeError as err:
                print(err)
        except:
                print("Invalid input.")
    
    print("Your age:", age, "years old.")
    print("Your height:", height, "inches tall.")
    print("Your weight", weight, "lbs")
    
    print("Your ideal fat burning heart rate is", float(round((220 - age) * .7)), "BPM.")
    print("Your Body Mass Index is", float(round((703 * (weight / height**2)))))

I tried giving each block a while loop but it keeps doing the first loop asking for the age even when a valid input is given.

This was the example output given for the assignment:

Welcome to the Weber State University Human Performance Lab!
Please utilize the following calculator to find your ideal fat burning heart rate and BMI.

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 positive.
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
 4  49  4
1 Jan 1970

Solution

 0

you can create a get_valid_input function like this

def get_valid_input(prompt, error_message, value_type=float, positive=True):
    while True:
        try:
            value = value_type(input(prompt))
            if positive and value <= 0:
                raise ValueError(error_message)
            return value
        except ValueError:
            print(error_message)

which you can use to ask and validate user input

# Get valid age
age = get_valid_input(
    "Please enter your age: ",
    "Invalid age. Must be a number greater than zero."
)
2024-07-10
Edgar Ortega