In: Computer Science
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
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: