Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using the Wrong Indentation

00:00 Using the Wrong Indentation. There are two sub-classes of SyntaxError that deal with indentation issues specifically. The first that you’ll see is IndentationError. While other programming languages use curly braces to denote blocks of code, Python uses whitespace.

00:17 That means that Python expects the whitespace in your code to behave predictably. It will raise an IndentationError if there’s a line in a code block that has the wrong number of spaces. While this may not be immediately apparent, line 5 is only indented by three spaces.

00:34 It should be in line with the for loop statement, which is four spaces across. Thankfully, Python can spot this easily and will quickly tell you what the issue is. There’s also a bit of ambiguity here, though.

00:46 Is the print('done') line intended to be after the for loop, or inside the for loop block? Let’s run the code and see the error.

00:58 Even though the traceback looks a lot like the SyntaxError traceback, it’s actually an IndentationError. The error message is very helpful and it tells you that the indentation level of the line doesn’t match any other indentation level. In other words, print('done') is indented by three spaces, but Python can’t find any other line of code that matches this level of indentation.

01:19 You can fix this quickly by making sure the code lines up with the expected indentation level. Here, this line can easily be corrected by adding a single space, bringing it into line with the for statement, and making sure that this happens after the for loop has completed.

01:40 The second indentation-related error is the TabError, which you’re going to see whenever there is a line that contains either tabs or spaces for its indentation, while the rest of the file contains the other.

01:51 This may not be noticeable until Python points it out to you.

01:56 Here, you can see some code, which looks perfectly okay. While the indentation is quite large, it’s the same for all of the lines inside the function, and this looks like it should work. However, one of the lines is indented with a tab, and the others are indented using spaces. This will only become apparent when you either inspect the code closely, or you ask Python to run it, as you’re going to see next.

02:24 As you can see, Python has raised a TabError saying there is inconsistent use of tabs and spaces in the indentation. This requires further investigation on our part.

02:35 This is one area where most modern code editors which understand Python syntax won’t let you make this kind of mistake. I’ve actually had to do this in a different editor to allow this to occur.

02:47 But when we highlight the code, we can see that, in fact, there is an arrow here showing that line 3 has a tab, whereas the other lines have spaces.

03:00 In this particular case, tabs have been set to eight spaces, so they look identical. However, this is easily fixed, in this case, by highlighting the unwanted tab and then using the Tab key in the editor.

03:15 Now, highlighting this code will show that this is now spaces instead. Saving this and running it

03:24 will show that the code is now functional.

Become a Member to join the conversation.