Cause 1 of 4
The line has stray leading whitespace.
Line it up with the statement above it. The traceback prints the file and the line number, and the caret marks the column — the fix is almost always on that exact line, not before it.
Python · 4 common causes
Updated
A line is indented further than the line before it without anything having opened a new block.
File "/app/main.py", line 4
print(total)
IndentationError: unexpected indentPaths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is IndentationError: unexpected indent.
Cause 1 of 4
Line it up with the statement above it. The traceback prints the file and the line number, and the caret marks the column — the fix is almost always on that exact line, not before it.
Cause 2 of 4
Python 3 rejects inconsistent indentation even when it looks aligned on screen. Set the editor to insert 4 spaces per indent and convert the file (in VS Code: Convert Indentation to Spaces); PEP 8 is 4 spaces and no tabs. Python 3 raises TabError for an ambiguous tab/space mix on its own, so no extra flag is needed; python -t was a Python 2 option and is a silent no-op here. To see which character is which, switch on the editor's render-whitespace setting, or run cat -A script.py where a tab prints as ^I.
Cause 3 of 4
Select the pasted block and reindent it as a unit — most editors do this with Shift+Tab or a Reindent Lines command. Pasting into a REPL adds its own layer of trouble; use %paste in IPython or paste into a file instead.
Cause 4 of 4
Python does not join lines by indentation. Wrap the whole expression in parentheses — the contents can then span as many lines as you like — or, less readably, end the previous line with a backslash and no trailing space.
Let a formatter own indentation entirely: run Black or Ruff on save, and enable the editor setting that renders whitespace so a tab is visible before you commit it.
Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.
Open the editor