The most embarrassing debugging story I've ever told. And the lesson every Python beginner needs to hear.
The Day That Broke Me
I remember it clearly.
I was building a REST API in Python. Everything was going well. I had the routes set up, the logic written, the database connected.
I ran the code.
Error.
I fixed what I thought was wrong.
Ran it again.
Different error.
Fixed that.
Ran it again.
Another error.
This went on for an entire day.
A FULL DAY. On one problem.
I Googled everything. I read Stack Overflow threads. I rewrote entire sections of code. I questioned every life decision that led me to programming.
And then — at the end of the day — an error message finally showed me exactly what was wrong.
It took me 30 seconds to fix.
30 seconds. After a full day of suffering.
The culprit? A single indentation error. 😅
What IndentationError Actually Means
If you're new to Python — IndentationError is Python's way of saying:
"Your code structure doesn't make sense to me."
Python uses indentation — the spaces or tabs at the beginning of lines — to understand code structure. Unlike JavaScript or Java which use curly braces {} — Python uses whitespace itself.
# JavaScript style (curly braces)
function greet() {
console.log("Hello");
}
# Python style (indentation)
def greet():
print("Hello") # This MUST be indented
If your indentation is wrong — Python gets confused and throws an error.
Simple concept. Painful in practice.
My Actual Code — The Problem
Here's a simplified version of what I was building. A basic API endpoint in Python:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}")
def get_user(user_id: int):
user = {
"id": user_id,
"name": "Prashik",
"email": "prashik@example.com"
}
return user
Can you spot the error?
Look carefully.
Take your time.
...
...
...
Did you see it?
The user = { line is not indented. It should be inside the get_user function — but it's sitting at the same level as the function definition itself.
Python saw this and completely lost the plot.
The Errors That Confused Me All Day
Here's the thing about IndentationError — it doesn't always point to the REAL problem.
Sometimes it points to the line AFTER the problem. Sometimes it says something confusing like:
IndentationError: expected an indented block after function definition on line 6
Or worse:
IndentationError: unindent does not match any outer indentation level
I kept looking at the lines Python was complaining about. Fixing those lines. Getting new errors. Getting more confused.
I was fixing the SYMPTOMS not the CAUSE.
The Special Villain — Tabs vs Spaces
After a few hours of confusion I made things worse.
I started mixing tabs and spaces.
Some lines I pressed Tab. Some lines I pressed Spacebar four times. To my eyes they looked identical. Same amount of space. Same visual indentation.
But to Python they are COMPLETELY different.
def calculate(x, y):
result = x + y # indented with SPACES
return result # indented with TAB
These look the same visually. Python sees them as completely different indentation levels and throws:
TabError: inconsistent use of tabs and spaces in indentation
I had introduced this problem while trying to fix the original problem.
Now I had TWO problems.
What I Tried That Didn't Work
Attempt 1 — Rewriting the function
I rewrote the entire function from scratch. Same error. Because I copied my bad habits into the new code.
Attempt 2 — Adding spaces everywhere
I added extra spaces to lines I thought were wrong. Made things worse.
Attempt 3 — Removing spaces everywhere
Removed indentation from random lines. Broke everything completely.
Attempt 4 — Googling "Python indentation error fix"
Got 50 different answers. None of them matched my exact situation.
Attempt 5 — Restarting VS Code
Classic desperate move. Did nothing. 😄
Attempt 6 — Rewriting the entire file
Yes. I rewrote my entire API file from scratch. And because I didn't understand the ROOT problem — I made the same mistake again.
The Error Message That Finally Saved Me
Late in the evening — exhausted and frustrated — I ran the code one more time.
This time I read the error message MORE carefully than I had all day.
File "main.py", line 7
user = {
^
IndentationError: expected an indented block after function definition on line 5
Something clicked.
"Expected an indented block AFTER function definition on line 5."
Line 5 was my function definition — def get_user(user_id: int):
Python was saying — after this function definition I expected INDENTED code. But the very next line user = { was NOT indented.
I added 4 spaces before user = {.
Ran the code.
It worked.
30 seconds.
After a full day.
I sat there staring at the screen for a full minute. 😶
The Fixed Code
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}")
def get_user(user_id: int):
user = { # ← 4 spaces indentation. THIS was missing
"id": user_id,
"name": "Prashik",
"email": "prashik@example.com"
}
return user # ← also properly indented
That's it. Four spaces. One day of my life.
The Real Lessons
Lesson 1 — Read error messages completely
I was reading the first line of error messages and ignoring the rest.
The most useful information is usually at the BOTTOM of the error — the specific file and line number with the little arrow ^ pointing exactly at the problem.
File "main.py", line 7
user = {
^
IndentationError: expected an indented block after function definition on line 5
That arrow ^ is your best friend. It's pointing directly at the problem. I ignored it for hours.
Lesson 2 — NEVER mix tabs and spaces
Pick one. Stick to it forever.
The Python community standard is 4 spaces for indentation. Not tabs. Not 2 spaces. Not 8 spaces. 4 spaces.
Set your editor to automatically convert tabs to 4 spaces. In VS Code:
Settings → Editor: Insert Spaces → ON
Settings → Editor: Tab Size → 4
Now every time you press Tab — it inserts 4 spaces automatically. Problem solved forever. ✅
Lesson 3 — Use a linter from day one
A linter is a tool that checks your code for errors BEFORE you run it.
Install pylint or flake8:
pip install pylint
pylint main.py
It would have caught my indentation error instantly. Before I even ran the code.
VS Code also has built-in Python linting — just install the Python extension and it underlines errors in real time. Red squiggly lines = something is wrong. Don't ignore them.
Lesson 4 — The problem is rarely where you think it is
I kept looking at the lines Python complained about. But the real problem was always ONE line before.
When Python says "error on line 7" — the CAUSE is often on line 6 or 5 or even earlier.
Always look at the lines BEFORE the error too.
Lesson 5 — Step away when frustrated
Around hour 6 I was so frustrated I couldn't think straight.
If I had taken a 30 minute break and come back with fresh eyes — I probably would have spotted the problem in 5 minutes.
When debugging drives you crazy — walk away. Drink water. Come back. Fresh eyes see things tired eyes miss.
The Most Common IndentationError Situations
After my experience I researched this deeply. Here are the most common situations:
Situation 1 — Forgot to indent after function definition
# WRONG ❌
def greet():
print("Hello") # Should be indented
# RIGHT ✅
def greet():
print("Hello")
Situation 2 — Forgot to indent after if statement
# WRONG ❌
if age > 18:
print("Adult") # Should be indented
# RIGHT ✅
if age > 18:
print("Adult")
Situation 3 — Inconsistent indentation
# WRONG ❌
def calculate():
x = 10
y = 20 # Different indentation level!
return x + y
# RIGHT ✅
def calculate():
x = 10
y = 20
return x + y
Situation 4 — Mixed tabs and spaces
# WRONG ❌ (looks same but isn't)
def greet():
print("Hello") # spaces
print("World") # tab
# RIGHT ✅ (consistent spaces)
def greet():
print("Hello")
print("World")
Situation 5 — Empty function body
# WRONG ❌
def coming_soon():
# TODO: implement later
# RIGHT ✅ — use pass for empty functions
def coming_soon():
pass
Your Debugging Checklist for IndentationError
Next time you see IndentationError — go through this list:
☐ Read the FULL error message including the arrow ^
☐ Look at the line the arrow points to
☐ Look at the line BEFORE that line too
☐ Check if you forgot to indent after def/if/for/while/class
☐ Check for mixed tabs and spaces
☐ Check for inconsistent indentation levels
☐ Enable linting in your editor
☐ If still stuck — take a break and come back
The Silver Lining
That painful day taught me more about Python than a week of tutorials would have.
I never make indentation mistakes anymore. My editor is configured perfectly. I read error messages completely and carefully. I use a linter on every project.
The most frustrating debugging sessions always teach the most valuable lessons.
If you're going through something similar right now — you're not bad at programming. You're just learning. Every developer has their version of this story.
Mine just happened to take a full day. 😄
Final Thought
The next time you're stuck on a bug for hours — remember this:
The fix is probably simpler than you think.
Read the error message one more time. Carefully. All of it.
The answer is usually right there — pointing at you with a little arrow ^
You just have to look. 💪
Follow LearnWithPrashik for more honest developer stories and practical Python content.
Connect with me:
LinkedIn: linkedin.com/in/prashik-besekar
GitHub: github.com/prashikBesekar
Top comments (0)