我试图在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')
打开任何块后给出4个空格来缩进该块,尝试它。 – bhansa
通过修复缩进块来修复缩进块错误。 Python依靠缩进来描述哪些代码在哪个块中。如果你在'if'中有一个'if'而没有缩进,并且说'elif',那么Python怎么知道'elif'是用来干什么的呢? Python如何知道第二个“if”是在第一个“if”之内还是之外?缩进明确表示你的意思。 – zondo
一个基本的教程可能会让你感到痛苦。 [官方教程](https://docs.python.org/3.5/tutorial/introduction.html#first-steps-towards-programming)对缩进有这样的说法:“缩进是Python对语句进行分组的方式。 ..]请注意,基本块内的每一行必须缩进相同的数量。“ – syntonym