pyschools专题3第6题

2024-07-03 06:40:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解决这个问题:

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).'

但是我用这个代码使私有测试用例失败

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

我知道代码不是很漂亮,但它经历了很多尝试和错误。我考虑过添加代码来检查给定的数字中是否有一个是零,但还是失败了。在

我没看到什么?在


Tags: andofyourisriskbmiheightweight
3条回答
if bmi >= 25.5:

应该是

^{pr2}$
if bmi >= 25.5:
    retv += " (High Risk)."

您指定>;=25.5,但在说明中,它指定BMI超过27.5的高风险

答案如下:

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

相关问题 更多 >