[Solved] IndentationError: expected an indented block after function definition
Introduction:
- An
IndentationErroroccurs when Python expects an indented block of code after a function definition, but the block is missing or improperly indented. This tutorial will help you understand and fix this common error.
Example of IndentationError:
Consider this program where the body of the function
addition()is not properly indented:pythondef addition(a, b): return a + b print(addition(10, 20))
Output:
File "answer.py", line 2
return a + b
^
IndentationError: expected an indented block after function definition on line 1
- Python clearly indicates that it expects an indented block after the function definition.
Solution:
To fix this error, ensure the statements within the function are properly indented. Here is the corrected program:
pythondef addition(a, b): return a + b print(addition(10, 20))
Output: 30
Summary:
- In this tutorial, we learned how to solve the
IndentationErrorthat occurs when the body of a function is not properly indented. Proper indentation is crucial in Python to define code blocks and ensure the program runs correctly. Practice writing and indenting functions to avoidIndentationErrorin your Python programs!