Can somebody help to identify my mistake; I don't understand why this code fails to pass the test case

Question:

The main task is to write a rudimentary BMI calculation function called 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:

1.  "Under" : if the BMI is lower than 18.5 (exclusive)
2.  "Normal": if the BMI is higher than 18.5 (inclusive) but lower than 25 (exclusive)
3.  "Over": if the BMI is higher than 25 (inclusive) but lower than 35 (exclusive)
4.  "Obese": if the BMI is higher than 35 (inclusive)

Suppose that the height is 1.7 (meters) and weight is 68 (kilograms), function call compute_bmi() will read 1.7 and 68 from keyboard and then return string "Normal". In addition, the height and weight should be converted to float type. For this particular task, the server will read data from keyboard using input(), and that the compute_bmi() function is supposed to return a string instead of printing it on screen.

I have included the try-exception block code to avoid a division of 0 if the value of the height is equal to 0.

My code:

def compute_bmi():
    
    BMI = weight / (height **2)

    if BMI < 18.5:
        return('Under')

    elif 18.5 <= BMI < 25:
        return('Normal')

    elif 25 <= BMI <35:
        return('Over')

    else:
        return('Obese')

height = float(input("Enter height in m: "))
weight = float(input("Enter weight in kg: "))

if height <= 0 or weight <= 0:
    print("Please enter a valid number.")
    
else:
    try:
        print(compute_bmi())
    except ValueError:
        print("Please enter a valid number.")

I’ve tried numerous times rewriting the code in a different format, but still it fails to pass the test case.

The test case:

Expression                           Expected   
check_bmi(['1.7','68'])              'Normal'   
check_bmi(['1.7', '100'])            'Over' 

In my opinion, I believe there could be some inherent errors in the test case, since they’re using the function check_bmi rather than the original function which is stated compute_bmi. On the flipside, it is possible that the test case is testing some other aspect of the function that is not covered by the code provided, or there might be some mistake in the test cases that you are using to test the function, and the expected output of the function is different from the output I am seeing.

Asked By: programming_is_fun

||

Answers:

You need to pass 2 parameters in the function for height and weight

You are taking 2 inputs from the user and converting them to float. Later those inputs are passed to compute_bmi(a,b) via compute_bmi(height,weight) and you are getting the return from this function.

This should be the code:

def compute_bmi(a,b):
    
    BMI = weight / (height **2)

    if BMI < 18.5:
        return('Under')

    elif BMI < 25:
        return('Normal')

    elif BMI < 35:
        return('Over')

    else:
        return('Obese')

height = float(input("Enter height in m: "))
weight = float(input("Enter weight in kg: "))

if height <= 0 or weight <= 0:
    print("Please enter a valid number.")
    
else:
    try:
        print(compute_bmi(height,weight))
    except ValueError:
        print("Please enter a valid number.")

When you take input from user:

Enter height in m: 1.7
Enter weight in kg: 68
Normal

Enter height in m: 1.7
Enter weight in kg: 100
Over
Answered By: God Is One
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.