Book a free demo

Maths, coding, AI & parents

How to debug Python code: the six errors every beginner meets

Learn to read Python error messages and fix the six mistakes beginners make most: syntax errors, name errors, indentation, type errors, index errors and dividing by zero. With examples and a debugging checklist.

Updated ยท 3 min read ยท By the Ashkam team

ShareWhatsApp

Every programmer, from a ten-year-old to a professional, writes code that does not work the first time. Fixing it is called debugging, and it is a skill you can practise. The most important habit is simple: read the error message. It usually tells you the kind of mistake and the line number.

The six errors you will meet first

1. SyntaxError: a rule of the language was broken

Python has grammar rules. If you forget a colon, a bracket or a quote, it cannot understand your code.

if age > 10
    print("big")

The if line needs a colon at the end: if age > 10:. Check the line named in the message and the line just before it. Missing brackets and quotes are very common.

2. NameError: Python does not know that name

You used a name that does not exist, usually because of a typo or because you used it before you made it.

score = 5
print(scor)

scor is not score. Check the spelling and capital letters: Python treats Name and name as different.

3. IndentationError: the spaces are wrong

Python uses the spaces at the start of a line to know which lines belong together. Lines inside a loop, an if or a function must be indented by the same amount, usually four spaces.

for i in range(3):
print(i)

The print line must be indented under the for.

4. TypeError: the wrong kind of value

You tried to mix things that do not go together, such as adding a number to text.

age = 10
print("Age: " + age)

Turn the number into text with str(age), or use an f-string: print(f"Age: {age}").

5. IndexError: that position does not exist

You asked for an item that is not in the list. Lists start counting at 0, so a list of three items has positions 0, 1 and 2.

colours = ["red", "green", "blue"]
print(colours[3])

The last position is colours[2], or colours[-1].

6. ZeroDivisionError: you divided by zero

Dividing by zero has no answer, so Python stops. Check the number before dividing, or use try and except as shown in the Python cheat sheet.

A debugging checklist

  1. Read the message. Note the error name and the line number.
  2. Look at that line, and the one before it. Many errors are caused by the line above.
  3. Print things. Add print(variable) to see what is really inside a variable.
  4. Make the problem smaller. Delete or comment out lines until the bug goes away, then add them back one at a time.
  5. Explain it out loud. Describing the code line by line to a friend, or a toy, often reveals the mistake. Programmers call this rubber-duck debugging.
  6. Take a break. Fresh eyes spot mistakes that tired eyes miss.

Bugs that do not show errors

Sometimes the program runs but gives the wrong answer. This is a logic error. For example, range(1, 10) counts to 9, not 10, because the last number is not included. The way to find these is to work out by hand what the answer should be, then use print to compare.

Practise

Try the Python programs, change a line and see what error you get. Breaking code on purpose and fixing it is one of the quickest ways to learn. The predict the output game is also good practice at reading code carefully.

Common questions

Is it bad to get errors?

No. Errors are how Python tells you what to fix. Experienced programmers see errors every day.

Why does Python say the error is on a line that looks right?

Sometimes the real mistake is on the line before, such as a missing bracket. Python only notices when it reaches the next line.

How do I stop making typing mistakes?

Slow down, use the same names everywhere, and read your code before you run it. Practice with typing games helps too.

ShareWhatsApp

This guide is general information. For exams, follow your school, teacher and official exam board, and use your own judgement about what suits your child.

Keep reading

Related guides

Start a conversation

Tell us about the learner.

A parent or guardian can share the age range, level and goals, and we'll suggest a starting point and confirm the format before anything is booked.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Take a break and play

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try