What is unexpected indent in Python?As the error message indicates, you have an indentation error . This error occurs when a statement is unnecessarily indented or its indentation does not match the indentation of former statements in the same block. Python not only insists on indentation, it insists on consistent indentation . You are free to choose the number of spaces of indentation to use, but you then need to stick with it. If you indent one line by 4 spaces, but then indent the next by 2 (or 5, or 10, or ...), you'll get this error. Whenever you have a situation with code inside of a statement, that inside code must be indented, and must be indented consistently. It is used by the interpreter to know how to delimit blocks of instructions.
How to indent my code?The basic rule for indenting Python coding style is: The first statement in a basic block, and each subsequent statement after it must be indented by the same amount. For example, the second line in the program below is unnecessarily indented:
numbers = "12345678" num = numbers print(num)output
In order to fix this error is to first make sure the problematic line even needs to be indented. For example, the above example using num = numbers can be fixed simply be unindenting the line:
numbers = "12345678" num = numbers print(num)However, if you are sure the line does need to be indented, the indentation needs to match that of a former statement in the same block . Run your code with the -tt option to find out if you are using tabs and spaces inconsistently. The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.
Python IndentationError and tabsSpaces are the preferred indentation method. But, Python realizes that some people still prefer tabs over spaces and that legacy code may use tabs rather than spaces, so it allows the use of tabs as indentation. However, by default, mixing tabs and spaces is still allowed in Python 2 , but it is highly recommended not to use this "feature". Python 3 disallows mixing the use of tabs and spaces for indentation. Replacing tabs with 4 spaces is the recommended approach for writing Python code .
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3