IndentationError: unexpected indent
In Python programming, indentation is important. But the language emphasizes consistent indentation. So if you indent your code using 4 spaces, this has to be maintained throughout the code. While coding, you might encounter an error called “IndentationError unexpected indent”.
The way to fix this error is to ensure that all the lines of code are indented using the same number of whitespaces. This means if you are starting off with 4 spaces, you cannot use 2 or 3 spaces elsewhere.
Let us take a look at some examples to understand this bug a little better.
a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
if b > a:
print("B is greater than A")
elif a == b:
print("A and B are equal")
else:
print("A is greater than B")
Output:
File "ind.py", line 5
elif a == b:
^
IndentationError: unexpected indent
In the above example, as you can see at line no. 5, we have assigned some extra space indent which makes this block code separate(not identical) from other block codes.
This error is encountered as the indentation of the entire code is not consistent. In this code, the indentation of the first return statement is incorrect. The rest of the code has a proper indentation.
Solution:
a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
if b > a:
print("B is greater than A")
elif a == b:
print("A and B are equal")
else:
print("A is greater than B")