我試圖在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