Fix: Python IndentationError: unexpected indent


The one thing that makes Python different then most other programming and scripting languages is its strictness towards code indentation. If the code is not well indented, your code will not execute and you will see a IndentationError.

Code indentation is a Python language's design philosophy

The IndentationError exception class is the base class of syntax errors related to incorrect indentation. It is a subclass of SyntaxError.

To understand it better, one should take a look at the PEP 8 Indentation section.

As per PEP 8 instructions - "We should use 4 spaces per indentation level."

Now let's try to figure out IndentationError: unexpected indent with a simple Python code example.

Example:
print("hello there")
    print("how are you?")
Exception:
File "<ipython-input-16-2298b55469ab>", line 2
    print("how are you?")
    ^
IndentationError: unexpected indent

Why unexpected indent exception?

    In Python, we should add an indentation (4 spaces per indentation level per PEP 8) to indicate the beginning or ending of code blocks, such as,

    • For Loops

      Example:
      numbers = [84, 87, 76, 92, 14, 87, 98]
      
      for number in numbers:
          print(number) # 4 space indentation!
    • While Loops

      Example:
      numbers = [84, 87, 76, 92, 14, 87, 98]
      index = 0
      
      while index < len(numbers):
          print(numbers[index])  # 4 space indentation! for the block!
          index += 1
    • If-Else Conditional Statements

      Example:
      numbers = [84, 87, 76, 92, 14, 87, 98]
      
      for number in numbers:
          if number % 2 == 0:
              print(f"{number} is even")
          else:
              print(f"{number} is odd")
      
    • Function definition Example:
      def print_even_odd(numbers):
          for number in numbers:
              if number % 2 == 0:
                  print(f"{number} is even")
              else:
                  print(f"{number} is odd")
      
      numbers = [84, 87, 76, 92, 14, 87, 98]
      print_even_odd(numbers)
      
    • Try-Except Blocks

      Example:
      def print_even_odd(numbers):
          try:
              for number in numbers:
                  if number % 2 == 0:
                      print(f"{number} is even")
                  else:
                      print(f"{number} is odd")
          except TypeError:
              print("Error: The input should be a list of numbers")
      
      numbers = [84, 87, 76, 92, 14, 87, 98]
      print_even_odd(numbers)
      

    As you would have understood, the two lines we have is neither among the above cases, thus the second line cannot have a intention, thus causing IndentationError: unexpected indent

Python - IndentationError- unexpected indent Exception
Tip:

One should make use of IDE's such as PyCharm or Visual Studio Code that help to fix indentation errors with their built-in features that does automatic indentation correction, real-time error highlighting, and code formatting options. This will not only saves time but also contributes to producing clean, well-organized code that is easier to read and maintain.


Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap