0

[solved]My bmi calculator gives an error saying line 6 Expected an indented block..it's not .

#your code goes here weight = float(input()) height = float(input()) bmi= weight/(height**2) if bmi<18.5: print('Underweight') elif bmi>=18.5 and bmi<25: print('normal') elif bmi>=25 and bmi<30: print('overweight') else bmi>=30: print('obesity')

20th May 2021, 9:56 PM
Shruti sharma
Shruti sharma - avatar
26 Antworten
+ 1
indentation is very important in python: that's what determine where a code block start and end (while most of other languages uses curly brackets do delimit them -- indentation is then optionnal but recommended for readability) ^^ also indentation must be consistent: if you use tabs, you must have only tabs, if you prefere x spaces, you must use same x numbers of spaces... an if..else statement should look like: if condition: # line of code # line of code else: # line of code # line of code even if you're not required to use same spacement for different blocks, it is highly recommanded ;)
20th May 2021, 10:08 PM
visph
visph - avatar
+ 1
anyway, you don't need to check values previously checked: first if test for bmi<18.5 so if that the case, the related block will be executed and other elif/else where never reached. if that's not the case, next elif could only test if bmi<25, because you are sure that bmi>=18.5 is True ^^ ... and so on for next all elif ;P
20th May 2021, 10:25 PM
visph
visph - avatar
+ 1
wich is the line 11 in your source?
20th May 2021, 10:32 PM
visph
visph - avatar
+ 1
oh, I see: the last else do not have a condition ^^
20th May 2021, 10:33 PM
visph
visph - avatar
+ 1
Shruti sharma Here is some mistakes in your code: 1 - Indentation is very important in python 2 - else never has condition, else only has expression 3 - First letter should be Capital of each word. Here is solution: #your code goes here weight = float(input()) height = float(input()) bmi = weight / (height ** 2) if bmi < 18.5: print('Underweight') elif bmi >= 18.5 and bmi < 25: print('Normal') elif bmi >= 25 and bmi < 30: print('Overweight') else: print('Obesity') #Now saying that try again then only u can become a good coder
21st May 2021, 4:15 AM
A͢J
A͢J - avatar
+ 1
Yes it's done now only one thing was getting wrong which u mentioned above that it should be in capital and the code runned..😃😃yipeeeee....thanks to u all for helping me out and finding me what's mistake is being done by me...
21st May 2021, 8:05 AM
Shruti sharma
Shruti sharma - avatar
+ 1
BTW I am a beginner too! Just writing my views here buddy! I hope you will fine some help with it. You haven't indented anything in your code my friend. In python, indentation is very very important, it defines, the blocks of a condition, So you need to indent the each print statement under any condition in your code! I.e #your code goes here weight = float(input()) height = float(input()) bmi = weight/(height**2) if bmi < 18.5: print('Underweight') elif bmi >= 18.5 and bmi < 25: print('Normal') elif bmi >= 25 and bmi <30: print('Overweight') else: print('obesity') 1 more thing to explain here, when each of the about four conditions doesn't met, then all the remaining conditions will come automatically under the 'else' statement, you don't need to put a condition there! Like in your case you had written (else bmi>=30)! There is no need for it!
22nd May 2021, 7:26 PM
Javed Ullah
Javed Ullah - avatar
+ 1
Shruti sharma You can add [SOLVED] before the question.
22nd May 2021, 7:38 PM
A͢J
A͢J - avatar
0
So sir/ mam what should I do to run my code..i mean that can't I use elif statement instead of writing else if separately as I have written.
20th May 2021, 10:18 PM
Shruti sharma
Shruti sharma - avatar
0
add spacing to the lines related to if, elif and else ;P
20th May 2021, 10:19 PM
visph
visph - avatar
0
weight = float(input()) height = float(input()) bmi= weight/(height**2) if bmi<18.5: print('Underweight') elif bmi>=18.5 and bmi<25: print('normal') elif bmi>=25 and bmi<30: print('overweight') else: print('obesity')
20th May 2021, 10:21 PM
visph
visph - avatar
0
Okk I will try once again..
20th May 2021, 10:22 PM
Shruti sharma
Shruti sharma - avatar
0
weight = float(input()) height = float(input()) bmi= weight/(height**2) if bmi<18.5: print('Underweight') elif bmi>=18.5 and bmi<25: print('normal') elif bmi>=25 and bmi<30: print('overweight') else bmi>=30: print('obesity') I made changes with spacing that issue is solved but now it's saying invalid syntax for line 11
20th May 2021, 10:31 PM
Shruti sharma
Shruti sharma - avatar
0
Else bmi >=30
20th May 2021, 10:33 PM
Shruti sharma
Shruti sharma - avatar
0
that's what I said ;P (don't forgot that pyrhon is case sensitive and 'else' must be all lowercased -- I guess it is capitalized un your last post because of your phone)
20th May 2021, 10:35 PM
visph
visph - avatar
0
But I have written that else bmi >=30:
20th May 2021, 10:35 PM
Shruti sharma
Shruti sharma - avatar
0
No no in code it's in lowered case else...I m sorry that I m disturbing u this time.
20th May 2021, 10:37 PM
Shruti sharma
Shruti sharma - avatar
0
yes, you must write only: else: (I will edit my previous post where I have just copied pasted your code and only add spaces)
20th May 2021, 10:37 PM
visph
visph - avatar
0
Still showing the same error on line 11..
20th May 2021, 10:40 PM
Shruti sharma
Shruti sharma - avatar
0
it must run fine now... are you sure to have put the colon after 'else' ?
20th May 2021, 10:42 PM
visph
visph - avatar