Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 5.26

bookIndentation: The Silent Bug

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 2
some-alt