Question

In: Computer Science

In the previous exercise we enhanced our program by creating a function that can be called...

In the previous exercise we enhanced our program by creating a function that can be called and executed when we want to. Anytime we need to calculate the BMI of a someone we can run the program. Now, let's think of the scenario where we need to calculate the BMI for 10 people for example. Then we would have to run the program 10 times. Every time repeating the command

python healthTools.py

Which is an okay way. But we can enhance the usability of our program even more by allowing us to enter how many people the program needs to run for and have it programmed to repeat that many times. This way weather we have 10 or 10000 it will keep going until it completes that many times.

Update healthTools.py to ask for how many users there are and based on this input repeat the interaction to capture (get) the input values for the height and weight for each person and output their BMI of that user.  The program interaction should be identical to exercises 3 and 4 the only difference is that we will repeat the interaction based on user input.

Save your program in a file called healthTools_v2.py. Upload your file to this question.

In some situations we do not know how many times we want to execute the program for. Imagine the scenario where this program will be used at an event that is open to the public to create promote healthy weight. The number of people is not known to start and can vary every time.

Another way to control the number of times a program runs is to ask after each time if we want to repeat the interaction. Update healthTools.py to ask after each it outputs the result of the BMI calculation to ask whether we want to enter another set of values. If the answer is "yes" we repeat if not the program stops. Save your program in a file called healthTools_v3.py. Upload your file to this question.

The next step is to ensure that our program is capable of handling some error that can result from bad user input.

Update your program to handle the situation if a user enters something other than a numeric value for the height and weight inputs. Name your file healthTools_v4.py. Upload your file to this question.

THE FOLLOWING IS MY PREVIOUS CODE FOR THE BMI CALCULATOR. PLEASE BASE YOUR ANSWERS OFF OF THIS BASE CODE.

def BMI (height, weight):
bmi = (weight*703) / (height**2)
return bmi
height = float(input('enter height in inches'))
weight = float(input(' Enter weight in pounds'))
bmi=BMI(height, weight)
if bmi < 18.5:
print('you are underweight')
elif 18.5 <= bmi < 24.9:
print('you are normal')
elif 24.9 <= bmi < 29.9:
print(' you are overweight')
elif bmi > 30.0 :
print('you are obese')

Thank you

Solutions

Expert Solution

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you need any change in code. Else please upvote

CODE FOR healthTools_v2.py

#Function to calculate and return the bmi

def BMI (height, weight):

    bmi = (weight*703) / (height**2)

    return bmi

   

num_users = int(input("Enter the number of users: "))     #Reading the number of users

for i in range(num_users): #Loop thgrough each user

    height = float(input('Enter user ' + str(i+1)+' height in inches: ')) #Reading the height of user

    weight = float(input(' Enter user ' + str(i+1)+' weight in pounds: ')) #Reading the weight of user

    bmi = BMI(height, weight) #Calling BMI()

    #Displaying the category

    if bmi < 18.5:

        print('you are underweight\n')

    elif 18.5 <= bmi < 24.9:

        print('you are normal\n')

    elif 24.9 <= bmi < 29.9:

        print(' you are overweight\n')

    elif bmi > 30.0 :

        print('you are obese\n')

OUTPUT:

CODE FOR healthTools_v3.py

#Function to calculate and return the bmi

def BMI (height, weight):

    bmi = (weight*703) / (height**2)

    return bmi

   

repeat = True

while repeat: #Loop through each user

    height = float(input('\nEnter height in inches: ')) #Reading the height of user

    weight = float(input('Enter weight in pounds: ')) #Reading the weight of user

    bmi = BMI(height, weight) #Calling BMI()

    #Displaying the category

    if bmi < 18.5:

        print('you are underweight\n')

    elif 18.5 <= bmi < 24.9:

        print('you are normal\n')

    elif 24.9 <= bmi < 29.9:

        print(' you are overweight\n')

    elif bmi > 30.0 :

        print('you are obese\n')

   

    while True:

        option = input("Do you want to enter another set of values? (Yes/No): ")

        if(option.lower() == 'yes'):

            repeat = True

            break

        elif (option.lower() == 'no'):

            repeat = False

            break

        else:

            print('Invalid option!!! Please re-enter...')

OUTPUT:

CODE FOR healthTools_v4.py

#Function to calculate and return the bmi

def BMI (height, weight):

    bmi = (weight*703) / (height**2)

    return bmi

repeat = True

while repeat:

    try:

        height = float(input('Enter height in inches: ')) #Reading the height of user

        weight = float(input('Enter weight in pounds: ')) #Reading the weight of user

        bmi = BMI(height, weight) #Calling BMI()

        #Displaying the category

        if bmi < 18.5:

            print('you are underweight\n')

        elif 18.5 <= bmi < 24.9:

            print('you are normal\n')

        elif 24.9 <= bmi < 29.9:

            print(' you are overweight\n')

        elif bmi > 30.0 :

            print('you are obese\n')

        repeat = False

     

    except ValueError:

        print("Height/Weight value can't be non-numeric!!!\n\n")

        repeat = True

OUTPUT:


Related Solutions

How can we get our population to be more active? What levels of exercise do we...
How can we get our population to be more active? What levels of exercise do we need to promote to help people gain the health-related benefits associated with exercise?
This is a continuation of our last discussion in some ways. In the previous discussion we...
This is a continuation of our last discussion in some ways. In the previous discussion we covered the options used as speculating and hedging. A good and simple way to price an option is a Binomial Tree. The idea is that each branch involves a random walk. It’s like flipping a coin heads or tails and the next time it all resets again to either heads or tails. The Binomial Tree and its construction can use one and two period...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' //...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' // This variable should be assigned a new object // In this object create three key:value pairs // The keys should be: 'name', 'city', 'favoriteAnimal' // The values should be strings associated with the keys. // return the variable 'aboutMe' } function exerciseTwo(animal){ // Exercise Two: In this exercise you will be given an object called 'animal' // Create a new variable called 'animalName' //...
In our lab experiment, we filtered CaCl and CaOH2 solutions that we prepared the previous week...
In our lab experiment, we filtered CaCl and CaOH2 solutions that we prepared the previous week and transferred both of the filtrates using a 25 ml pipette into two erlenmeyer flasks. We then prepared the standard HCl by pipeting 100 ml of HCl to prepare 250 ml of 0.04M HCl from the 0.1 M HCl solution that we initially had. We pipeted 100 ml of HCl into a 250 ml volumetric flask and diluted to the mark. The buret was...
Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that...
DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that asks the user to enter a maximum of 20 numbers. The program should store the numbers in an array and then display the following data: 1-The lowest number in the array. 2-The highest number in the array. 3-The total of the numbers in the array. 4-The average of the numbers in the array. PLEASE AND THANK YOU
If we recognize that our perceptions are always limited, what can we do to improve our...
If we recognize that our perceptions are always limited, what can we do to improve our perception making? How can we make our perceptions less limited?
C++ For the assignment, we will use the previous assignment’s program that determines whether a college...
C++ For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… For each new class entry that is taken, you will write out the information to a file. The file can be any .txt file name of your choosing. The file should be appended to each...
C++ For the assignment, we will use the previous assignment’s program that determines whether a college...
C++ For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… Convert all of the function’s parameters from pass by value to pass by reference. Accumulate totals for each of the class rooms that we are using for this program. When the program ends (user choice...
C++ For the assignment, we will use the previous assignment’s program that determines whether a college...
C++ For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… Convert all of the function’s parameters from pass by value to pass by reference. Accumulate totals for each of the class rooms that we are using for this program. When the program ends (user choice...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT