Your code will not run, or it runs and does the wrong thing. Almost every time, it is one of about eight causes — and Python has usually already told you which one.
This page is a triage guide. Start at the top and work down; most people find their answer in the first two minutes.
First: is there an error message?#
This splits the problem in half, so answer it before anything else.
- Yes, there is a red block of text. Read the last line. It names the problem. Jump to the eight usual causes below.
- No, it runs but the answer is wrong. That is a logic error. Skip to it runs but the answer is wrong.
- Nothing happens at all. Skip to nothing happens at all.
The two-minute checklist#
Before you search anything, do these four things in order. They resolve most problems on their own.
- Read the last line of the error out loud. Not the whole block — the last line. It names the error type and describes it in a short sentence.
- Look at the line number it gives you, and the line above it. A missing bracket on line 11 is reported on line 12, because that is where the statement stopped making sense.
- Print the value you assumed. Put
print("DEBUG:", thing, type(thing))immediately above the failing line. Nine times out of ten the value is not what you thought. - Change one thing, then run it again. Changing three things at once means you will not know which one mattered.
The full method is in how to read programming errors.
The eight usual causes#
1. You forgot to convert input to a number#
The single most common first-week problem. input() always gives you text, even when the person typed digits.
age = input("Your age: ")
print(age + 1) # TypeError
print(int(age) + 1) # worksIt also causes a silent version, where nothing errors and a comparison simply never matches:
guess = input("Pick a number: ")
if guess == 7: # never true — "7" is not 7
print("Correct")2. A missing colon#
if age > 18 # SyntaxError: expected ':'
print("adult")Every if, elif, else, for, while, def and class line ends in a colon.
3. Indentation that does not line up#
Python decides what belongs inside a block purely by indentation, so spacing is not cosmetic.
for item in items:
print(item) # IndentationError: expected an indented blockIf you get TabError, you have mixed tabs and spaces. Set your editor to insert spaces and the problem never comes back — see VS Code setup for beginners.
4. A typo in a name#
NameError: name 'totl' is not definedPython is showing you the exact word it could not find. Also check that the name was created above the line using it, and that it was not created inside a function you are now outside of.
5. An unclosed bracket or quote#
This is the one where the reported line looks perfectly fine, because the real mistake is above it.
total = (price * quantity # opened, never closed
print(total) # error reported HEREWhen a SyntaxError points at a line you are certain is correct, look upward.
6. Asking for an item that does not exist#
items = ["a", "b", "c"]
print(items[3]) # IndexError: list index out of range
person = {"name": "Ada"}
print(person["email"]) # KeyError: 'email'
print(person.get("email", "-")) # safeThe last item of a list is at len(items) - 1, because positions start at 0.
7. The package is not installed for this Python#
ModuleNotFoundError: No module named 'requests'python -m pip install requestsUse python -m pip rather than plain pip — it guarantees the package installs for the same interpreter that will run your file.
8. You named your file after a library#
This one produces genuinely baffling errors. Save a file as random.py and import random imports your file instead of Python’s, so you get things like AttributeError: module 'random' has no attribute 'randint'.
Rename the file and delete the __pycache__ folder sitting next to it. Avoid random.py, email.py, json.py, string.py, math.py and test.py.
Every one of these messages is covered with more examples in common Python errors and how to fix them.
It runs but the answer is wrong#
No error means Python did exactly what you told it. What you told it was wrong. This is the harder kind, so be systematic.
- Narrow it down. Print values at three points in the program and find where the number stops being right. Then print between those two points. You will find it in a few rounds.
- Check the order of operations.
a + b / 2is not(a + b) / 2. - Check your comparison operators.
>when you meant>=is an entire class of off-by-one bugs. - Check the boundaries. Empty list, zero, one item, negative numbers, the last item of a loop. Bugs live at the edges.
- Check your types. Text sorts differently from numbers — as text,
"10"comes before"9".
scores = [72, 95, 61]
print("DEBUG scores:", scores)
total = sum(scores)
print("DEBUG total:", total)
average = total / len(scores)
print("DEBUG average:", average)Nothing happens at all#
- The window closes instantly on Windows. You double-clicked the file. Run it from a terminal instead, or add
input("Press Enter to close")at the bottom. - Your code is inside a function you never called. Defining a function does not run it — you need
greet()somewhere. - Your code is inside an
ifthat is never true. Print the condition’s value just above it. - It is stuck in a loop that never ends. Press Ctrl + C to stop it, then check that something inside the loop actually moves it towards its exit — see Python while loops.
- You are running the wrong file. Genuinely common. Check the filename in the terminal command against the file you have been editing.
Still stuck after twenty minutes#
Do these in order — the first two solve it surprisingly often.
- Explain the failing lines out loud, one at a time, as if to someone who does not know the problem. People routinely catch their own bug mid-sentence.
- Shrink it. Copy the failing part into a new empty file with fake values. If the small version works, the problem is in what you were feeding it.
- Search the error message with your own file names and values stripped out, so you match the general case.
- Ask, with details. Include the full error copied as text, the code around the reported line, what you expected, and what you already tried. See how to ask AI better programming questions.
Questions people ask#
Why does the error point at a line that looks fine?
Because Python only noticed the problem there. An unclosed bracket or quote on an earlier line means the statement never finished, so the complaint lands on the next line. Always check upward.
My code worked yesterday and does not today
Something changed — check in this order: are you in the same folder, is a data file missing or different, did you edit something and forget to save, and are you running the same Python. Print the values coming in before assuming the logic broke.
Should I wrap everything in try/except to stop the errors?
No. That hides bugs rather than fixing them, and you will meet the same problem later with no message to guide you. Use it for things genuinely outside your control — a missing file, bad user input, a failed network call. See Python try/except explained.
How do I know if it is my mistake or a bug in Python?
It is your mistake. Not always, but often enough that assuming so is the fastest route to a fix. The same goes for popular libraries — they are used by millions of people daily.
Is it normal to spend this long on one error?
Yes. Experienced developers still lose an afternoon to a typo occasionally. What changes with practice is not that you stop getting stuck — it is that you get unstuck faster and stay calmer while you do.
Where to go next#
- How to read programming errors — the method behind this checklist
- Common Python errors and how to fix them — every message, with fixes
- How to read a stack trace — when the error is several functions deep
- VS Code problems beginners hit — when the editor is the problem, not your code