Megimoo
Megimoo

Reputation: 397

pyschools Topic 3 Q 6

I'm trying to solve this question:

http://www.pyschools.com/quiz/view_question/s3-q6

Write a function using 'if/elif/else' conditionals to compute the BMI of a person, and return the risk associated with cardiovascular diseases.

BMI = weight(kg)/( height(m)*height(m) )

BMI Risk

27.5 and above High Risk

23 - 27.4 Moderate Risk

18.5 - 22.9 Low Risk

Below 18.5 Risk of nutritional deficiency diseases

Examples

HealthScreen(85, 1.75)

'Your BMI is 27.8 (High Risk).'

HealthScreen(68, 1.65)

'Your BMI is 25.0 (Moderate Risk).'

HealthScreen(60, 1.63)

'Your BMI is 22.6 (Low Risk).'

HealthScreen(40,1.58)

'Your BMI is 16.0 (Risk of nutritional deficiency diseases).'

However I fail the Private Test Cases with this code

def HealthScreen(weight, height):
    bmi = round(weight/(float(height)*height),1)
    retv = "Your BMI is " +str(bmi)
    if bmi >= 25.5:
        retv += " (High Risk)."
    elif bmi >=23 and bmi <=27.4:
        retv += " (Moderate Risk)."
    elif bmi >= 18.5 and bmi <=22.9:
        retv += " (Low Risk)."
    elif bmi <18.5:
        retv += " (Risk of nutritional deficiency diseases)."
    return retv

I know the code isn't very pretty, but it went trough a lot of trial and error. I thought about adding code to check if either of the given numbers is zero, but still failed.

What am I not seeing?

Upvotes: 0

Views: 1284

Answers (4)

Fasna
Fasna

Reputation: 576

def HealthScreen(weight, height):
height=float(height)
bmi=weight/height**2
if bmi>=27.5:
    return 'Your BMI is %.1f (High Risk).' %bmi
elif 27.4>bmi>23:
    return 'Your BMI is %.1f (Moderate Risk).' %bmi
elif 22.9>bmi>18.5:
    return 'Your BMI is %.1f (Low Risk).' %bmi
else:
    return 'Your BMI is %.1f (Risk of nutritional deficiency diseases).' %bmi

Upvotes: 0

Rodolfo Almaraz
Rodolfo Almaraz

Reputation: 1

Here is the answer:

def BMI(weight, height): 
BMI = round(weight/(float(height)*height),1)
return "%.1f" %BMI

Upvotes: 0

Christopher Markieta
Christopher Markieta

Reputation: 5913

if bmi >= 25.5:

Should be

if bmi >= 27.5:

Upvotes: 0

timc
timc

Reputation: 2174

if bmi >= 25.5:
    retv += " (High Risk)."

You specify >= 25.5 but in the instructions it specifies high risk is for a BMI over 27.5

Upvotes: 3

Related Questions