Editor

Python code checker

Find syntax errors, likely bugs and style problems in Python code, with the line to fix and why.

import os
import math
def average(numbers):
total = 0
for n in numbers:
total = total + n
return total / len(number)
def greet(name):
message = "Hello, " + name
print("Hello, " + name)
scores = [90, 85, 77]
if scores is []:
print("No scores")
print(average(scores))

Python is starting (a few seconds, the first time)...

Fix the code here and check again, or open it in the editor, which underlines problems as you type. Messy layout? Format it.

Common problems and how to fix them

MessageWhat it meansFix
SyntaxError: invalid syntaxPython cannot read the line: a missing colon, bracket or quote, or a typo in a keyword.Look at the line shown and the one before it.
IndentationError: unexpected indentA line is indented more than the block it belongs to.Line it up with the lines around it (4 spaces per level).
undefined name 'x'The name is used before it is defined or imported, or it is misspelt.Define it first, import it, or fix the spelling.
'os' imported but unusedAn import that the code never uses.Remove it, or use what it imports.
local variable 'm' is assigned to but never usedA variable that gets a value nobody reads.Use it, or remove the assignment.
'return' outside functionreturn can only end a function.Indent it into the function, or remove it.
"is" with 'list' literalis compares identity, so x is [] is always False.Use == to compare values, or not x to test for empty.
E225 missing whitespace around operatorPEP 8 puts a space on each side of =, +, == and the like.Write a = b + 1, not a=b+1.
E302 expected 2 blank linesPEP 8 puts two blank lines before a top-level function or class.Add the blank lines.
E501 line too longThe line is longer than the limit (79 in PEP 8).Break it inside brackets, or format the file.

Every error message, explained with examples: Python error explanations. To tidy the layout in one go, use the Python formatter.

Questions

What does the Python code checker find?

Three kinds of problem: syntax errors, found by Python's own compiler (with its warnings, such as is with a literal); likely bugs, found by pyflakes (undefined names, unused imports and variables, duplicate dictionary keys); and, if you tick Style, PEP 8 style notes from pycodestyle.

Does it run my code?

No. It reads the code without running it, so it is safe for code that asks for input or would run for a long time. To see what a program does, run it in the editor.

Can it find logic errors?

Not the ones that are valid Python: a wrong formula or an off-by-one loop passes every check. Run the program with a few test values, or step through it in the visualizer to watch each variable change.

Is my code sent to a server?

No. The checks run in your browser, with Python running on this page.