如何修复预期的缩进块错误?

时间:2016-03-27 11:20:32

标签: python visual-studio-2015

我试图用Python创建一个BMI计算器,但是当我运行该程序时,它说:' IndentationError:期望一个缩进的块'。它说错误在第7行,但是我错了什么?我使用的是Python 3.5.1

print ('Give your weight in kg: ')
weight_kg = float(raw_input())
print ('Give your length in meters: ')
length_meters = float(raw_input())
bmi = weight_kg / (length_meters * length_meters)
if bmi <= 18.5:
print ('Your BMI is'), bmi, ('what means that you have got underweight')
elif bmi > 18.5 and bmi <= 25:
print ('Your BMI is'), bmi, ('what means that you have got a normal weight')
elif bmi > 25 and bmi < 30:
print ('Your BMI is'), bmi, 'what means that you have got overweight')
elif bmi >= 30:
print ('Your BMI is'), bmi, ('what means that you have got obese')

1 个答案:

答案 0 :(得分:2)

在Python中,下级语句由其缩进级别标识,与大括号或某些其他语言使用的开始/结束关键字相反。因此,Python中的适当缩进不仅仅是样式约定,而且实际上是正确功能所必需的。在您的情况下,您需要缩进您的从属语句。请尝试以下方法:

print ('Give your weight in kg: ')
weight_kg = float(input())
print ('Give your length in meters: ')
length_meters = float(input())
bmi = weight_kg / (length_meters * length_meters)
if bmi <= 18.5:
    print ('Your BMI is', bmi, 'what means that you have got underweight')
elif bmi > 18.5 and bmi <= 25:
    print ('Your BMI is', bmi, 'what means that you have got a normal weight')
elif bmi > 25 and bmi < 30:
    print ('Your BMI is', bmi, 'what means that you have got overweight')
elif bmi >= 30:
    print ('Your BMI is', bmi, 'what means that you have got obese')