I am a beginner at programming, and now I have a problem changing the input of ( ) to the users' input.
Given a person’s weight (in kilograms) and height (in meter), his/her BMI (Body Mass Index) is calculated based on this formula:
BMI = Weight / Height2Write a function
compute_bmi()that reads in that order, the user's height (in meter) and weight (in kilogram), and returns a string that shows the body type of the user:
- "Under" : if the BMI is lower than 18.5 (exclusive)
- "Normal": if the BMI is higher than 18.5 (inclusive) but lower than 25 (exclusive)
- "Over": if the BMI is higher than 25 (inclusive) but lower than 35 (exclusive)
- "Obese": if the BMI is higher than 35 (inclusive) For example, suppose height is 1.7 (meters) and weight is 68 (kilograms), function call
compute_bmi()will read1.7and68from the keyboard and then return string"Normal".Be reminded that both height and weight should be converted to float type.
Note that in this question, we read data from the keyboard using
input(). For other questions in this assignment, data are passed to our functions as parameters (i.e. they don't useinput()). These are two different designs.
This is my programming, I tried it on IDLE and it works. but the assignment page shows it doesn't work.
def check_bmi():
mass = float(data[1])
height = float(data[0])
BMI = mass/height**2
if BMI < 18.5:
return 'under'
elif 18.5 <= BMI < 25:
return 'Normal'
elif 25 <= BMI < 35:
return 'Over'
else:
return 'Obese'
Can anyone give me some advice? Thanks a lot :D
CodePudding user response:
A note on comparisons:
if BMI < 18.5:
return 'under'
elif 18.5 <= BMI < 25:
return 'Normal'
elif 25 <= BMI < 35:
return 'Over'
else:
return 'Obese'
Consider that if the first condition isn't true, BMI must be greater than or equal to 18.5. And if the second isn't true, BMI must be greater than or equal to 25.
if BMI < 18.5:
return 'under'
elif BMI < 25:
return 'Normal'
elif BMI < 35:
return 'Over'
else:
return 'Obese'
You would read height and weight using input, but you have to convert that to float. Remember that BMI is weight divided by height squared.
def compute_bmi():
height = float(input())
weight = float(input())
BMI = weight / height ** 2
if BMI < 18.5:
return 'under'
elif BMI < 25:
return 'Normal'
elif BMI < 35:
return 'Over'
else:
return 'Obese'
CodePudding user response:
you are not returning anything
Below is the corrected code!
def check_bmi(weight, height):
BMI = weight/height*2
if BMI<18.5:
print ('under')
elif 18.5<=BMI<25:
print ('Normal')
elif 25 <= BMI < 35:
print ('Over')
else:
print ('Obese')
return
weight = float(input("Weight: "))
height = float(input("Height: "))
check_bmi(weight, height)
