Indentation: The Silent Bug
123def greet(name): message = "Hello, " + name print(message)
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.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 5.26
Indentation: The Silent Bug
Свайпніть щоб показати меню
123def greet(name): message = "Hello, " + name print(message)
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.
Дякуємо за ваш відгук!