Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Indentation: The Silent Bug | Syntax and Indentation Pitfalls
Common Python Mistakes and How to Fix Them

bookIndentation: The Silent Bug

123
def greet(name): message = "Hello, " + name print(message)
copy

Python uses indentation to define code blocks, such as those within functions, loops, and conditional statements. When you write a function like greet(name), every line that is part of the function body must be indented by the same number of spaces or tabs.

In the code sample above, Python reads the first line after the function definition—message = "Hello, " + name—and sees it is indented by four spaces. The next line, print(message), is indented by six spaces. Python expects all lines within the same block to have the same indentation, so when it finds a line that is indented differently, it raises an IndentationError.

This strict rule helps Python determine where blocks start and end, making code structure clear and unambiguous. Always ensure that each line within a code block uses consistent indentation to avoid silent bugs and errors.

Tips for Consistent Indentation in Python

  • Use only spaces or only tabs for indentation; never mix them;
  • Follow the PEP 8 recommendation: use four spaces per indentation level;
  • Configure your code editor to insert spaces when you press the tab key;
  • Enable editor features that highlight inconsistent indentation;
  • Always check indentation after copying or moving code blocks;
  • Watch for invisible whitespace — even a single extra space can cause an error.

Consistent indentation keeps your code readable and prevents hard-to-find bugs. Adhering to these practices ensures your Python code runs smoothly.

1. Which of the following code blocks uses correct indentation for the function body?

2. Arrange the lines to correct the indentation of the function so that it runs without errors.

question mark

Which of the following code blocks uses correct indentation for the function body?

Select the correct answer

question-icon

Arrange the lines to correct the indentation of the function so that it runs without errors.

def add(x, y):
Sum is: 7

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how to fix the indentation error in my code?

What happens if I mix tabs and spaces in Python?

Can you give more examples of correct indentation in Python?

Awesome!

Completion rate improved to 5.26

bookIndentation: The Silent Bug

Sveip for å vise menyen

123
def greet(name): message = "Hello, " + name print(message)
copy

Python uses indentation to define code blocks, such as those within functions, loops, and conditional statements. When you write a function like greet(name), every line that is part of the function body must be indented by the same number of spaces or tabs.

In the code sample above, Python reads the first line after the function definition—message = "Hello, " + name—and sees it is indented by four spaces. The next line, print(message), is indented by six spaces. Python expects all lines within the same block to have the same indentation, so when it finds a line that is indented differently, it raises an IndentationError.

This strict rule helps Python determine where blocks start and end, making code structure clear and unambiguous. Always ensure that each line within a code block uses consistent indentation to avoid silent bugs and errors.

Tips for Consistent Indentation in Python

  • Use only spaces or only tabs for indentation; never mix them;
  • Follow the PEP 8 recommendation: use four spaces per indentation level;
  • Configure your code editor to insert spaces when you press the tab key;
  • Enable editor features that highlight inconsistent indentation;
  • Always check indentation after copying or moving code blocks;
  • Watch for invisible whitespace — even a single extra space can cause an error.

Consistent indentation keeps your code readable and prevents hard-to-find bugs. Adhering to these practices ensures your Python code runs smoothly.

1. Which of the following code blocks uses correct indentation for the function body?

2. Arrange the lines to correct the indentation of the function so that it runs without errors.

question mark

Which of the following code blocks uses correct indentation for the function body?

Select the correct answer

question-icon

Arrange the lines to correct the indentation of the function so that it runs without errors.

def add(x, y):
Sum is: 7

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2
some-alt