Hi I am python newbie and I am working on NLP using python. I am having a error in writing if-else block in python. When I am writing only if block at that time it is workin
Getting the indentation correct isn't really a Python issue but rather an issue with the editor that you're using for your source code.
Most editors that understand Python will correctly add one level of indentation after a colon (as you're seeing). Because you can have as many statements as you want in that block of code, the editor has no way to know when to "outdent" the next line for the else
.
You have to tell the editor to outdent that line by hitting backspace or shift-tab on the line before starting to type.
If you are inserting the else
part after the rest of the code is written, make absolutely certain that the characters you use to indent are the same as for the if
statement. If the if
statement is indented with spaces, use the same number of spaces for else
. If the if
statement is indented with one or more tabs, use the same number of tabs for the else
statement. Don't mix spaces and tabs for indentation.
Don't assume that just because the lines "look" as if they're indented the same that they are indented the same. One may use spaces and one may use tabs (or some combination).