0

Why are we not using indentation?

Hey, why we not using indentation for "elif", in the following code: weight=float(input()) height=float(input()) BMI=weight/(height**2) if BMI < 18.5: print("Underweight") elif 25>BMI>=18.5: print("Normal") elif 30>BMI>=25.0: print("Overweight") elif BMI>=30.0: print("Obesity")

24th May 2021, 11:51 AM
Steven Pillay
Steven Pillay - avatar
4 Risposte
+ 6
Hi! I don’t know what you mean with ”we”, but Python doesn’t work without indentation at places in the code where it’s needed. For example in compounded statements. Read PEP8, if you want to be guided to get a good coding style for Python. (I suppose you have taken a course in Python already).
24th May 2021, 12:09 PM
Per Bratthammar
Per Bratthammar - avatar
+ 5
Steven Pillay It is perfectly indented because all elif have single line statement. If you put statement inside elif condition like this then you will get error. elif 25 > BMI >= 18.5: print ("Normal") print ("Normal") Here we will get error because elif have 2 statement see here this will give error: https://code.sololearn.com/ctS635GK0k2n/?ref=app but this will not give error https://code.sololearn.com/cfjhe26YOhgC/?ref=app
24th May 2021, 4:27 PM
A͢J
A͢J - avatar
+ 4
I think indentation is available in your code. Just put your code in codebits and then add it to here. You can see, this one is your code and indentation is already there in elif condition also. https://code.sololearn.com/cIKjBs6Hu10q/?ref=app
24th May 2021, 12:09 PM
axita
axita - avatar
+ 4
'elif' is not part of the code block after 'if' so it's indentation is the same as 'if' (and also 'else') if <condition 1>: code block 1 elif <condition 2>: code block 2 else: code block 3
24th May 2021, 3:42 PM
David Ashton
David Ashton - avatar