Indention is one of Python’s greatest strengths. It’s why Python code is usually so clean in comparison to what’s found in other programming languages. But at the same time, indention errors can be extremely perplexing for people who aren’t used to Python’s unique take on that concept. But if you’ve found yourself with an ” indentationerror: unexpected indent ” error you can rest assured that it’s a fairly easy fix once you know what’s going on.
What Is an Unexpected Indent?
We can begin by looking at the most probable explanation for an unexpected indent error. An unexpected indent problem typically stems from a conflict between an established indentation level and the rest of your python code. The first use of indentation in a code block sets the expectation for the rest of it. For example, if you used an indentation level of three for your first line within a function you can’t proceed to use four on the next. This explanation might make Python’s indentation system seem a little arbitrary at first. But looking a little deeper into the error highlights just how elegant Python indentation really is.
Looking Deeper Into Python’s Expectations for Indentation
One of the most important things to keep in mind about indentation is that Python’s interpreter insists on consistent indentation. People often stress how important it is to stick to particular guidelines when working with indentation in Python code. However, the most important rule is to simply stick with whatever indentation style you’ve begun with. That applies to both your codebase as a whole and any individual function or block of code. This can be best understood with a quick example. Type out and run the following python code.
def modOurValues(ourStartingNumber):
ourTotal = ourStartingNumber+998
print(“Indentation test”)
return ourTotal
print(modOurValues(1))
The first line creates a new function called modOurValues. The next line takes a value passed to it and adds it to 998 in order to generate an ourTotal variable. We then print out a quick line to highlight our progress in testing indentation. The function ends by returning the ourTotal calculation. The final line of code nests a call to the modOurValues function inside print. It’s concise, simple, and won’t even run past the first line.
This script highlights the fact that syntax and space are equally important in Python. The programming logic and syntax in this example are all correct. But lack of proper indentation and whitespace renders all that irrelevant. And a few small changes can show just how much space matters to Python’s interpreter. For example, highlight line two, three, and four. Add a tab spacing to each of them. Now run the script again. The script will now run without any errors. However, that’s only the beginning. Restore the code to its original state so that you can try another variation on this theme.
This time around try just adding one space to lines two, three, and four. You might expect this to trigger an indentation error because it flies in the face of most Python indentation guides. Most people are advised to stick with four spaces for indentation. Or, as a distant second choice, to use standard system tabs for indentation. But again, Python’s interpreter cares more about consistency than it does any particular choice in whitespace style. You can use four spaces for indentation, and most people do. But Python’s fine with any other number of spaces as long as you’re consistent with them. As seen if you add one more space to line two but not any of the others. Or, to make it more clear, if you format it like the following example.
def modOurValues(ourStartingNumber):
ourTotal = ourStartingNumber+998
print(“Space test”)
return ourTotal
print(modOurValues(1))
This example highlights the points about indentation levels that have been covered so far. On running this code you’ll see the ” Indentationerror: Unexpected Indent ” error message appear. But at this point it should be more clear why it’s appearing. And this will guide us to the best way to fix it.
How To Fix the Unexpected Indent Error
We’ve noted that a python indentationerror typically has more to do with consistency than a need for any particular number of spaces in your code. The Python error in the previous example is created by the disparity between line 2 of the script and the rest of the modOurValues function. The first line of a code block defines the outer indentation level. From that first point, the other lines need to work in accordance with the newly defined indentation. Whether that involves creating a Python list or other variable, working with conditionals, or anything else. All of the code needs to be created in alignment with the baseline set down by the first line of the code block.
However, with that fact in mind, it’s easy to fix this particular Python error. Simply add or remove spaces so that all of the code in the modOurValues function is lined up with each other. For example, we might decide to go by standard Python style conventions and use four spaces for each line. Try running the following, newly edited, example.
def modOurValues(ourStartingNumber):
ourTotal = ourStartingNumber+998
print(“Space test”)
return ourTotal
print(modOurValues(1))
The contents of modOurValues are now fully aligned with each other. And if you run this code you’ll see that the errors have disappeared. The two print statements output “Space test” and “999”. And with that completed, the script finishes successfully and closes.