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")
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).
+ 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
+ 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
+ 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