Reading Errors and Fixing Bugs

Python 3.14 · ✓ verified by execution on 2026-07-22

Syntax Errors vs. Exceptions

Errors in Python generally fall into two categories. First are Syntax Errors. Syntax errors, also known as parsing errors. This happens before the code is even run, because Python cannot parse what you wrote.

python
print('Missing parenthesis'
This example raises an error (on purpose)
SyntaxError

Second are Exceptions. Errors detected during execution. These happen while the code is running, even if the syntax is perfectly valid.

python
result = 10 / 0
print(result)
This example raises an error (on purpose)
ZeroDivisionError

Reading a Traceback

When an exception occurs, Python prints a traceback. This shows the context where the exception occurred.

python
def buggy_function():
    return 'string' + 5

buggy_function()
This example raises an error (on purpose)
TypeError

When you see a traceback, don’t panic! Always look at the bottom first. The last line of the error message indicates what happened.

python
print(unknown_variable)
This example raises an error (on purpose)
NameError

Built-in Exceptions

The first word on the last line of an error is the exception type. These exception names are built-in identifiers. Some common ones you will encounter include ValueError, TypeError, NameError, and IndexError.

python
print(int('hello'))
This example raises an error (on purpose)
ValueError
python
items = [1, 2, 3]
print(items[5])
This example raises an error (on purpose)
IndexError

Visualizing Exceptions

You can watch an exception halt execution and propagate using the visualizer:

python · visualize
try:
    def buggy_function():
        return 'string' + 5
    
    buggy_function()
except TypeError as e:
    print(f"Caught exception: {type(e).__name__}")

Check yourself

Are syntax errors and exceptions the exact same thing?

Reveal answer

No, syntax errors happen before code executes, while exceptions happen during execution. — Syntax errors happen before code executes because Python can't parse it. Exceptions happen while the code is running.

Where is the most important part of a Python error message located?

Reveal answer

At the very bottom of the traceback. — The most important part of a Python error—what actually happened—is located at the very last line of the traceback.

Are tracebacks useless jargon that should be ignored?

Reveal answer

No, they show the exact sequence of function calls that led to the error. — Tracebacks show the exact sequence of function calls that led to the error, providing the critical context needed to fix it.

Will errors in Python instantly crash your whole computer?

Reveal answer

No, they only crash the running script. — Errors in Python only crash the running script. The sandbox or IDE itself remains completely safe.

Challenges

Challenge 1 +50 XP

The following code has a syntax error. Fix it so it prints 'Hello'.

python
  • Test 1 — expects "Hello\n"
Need a hint? (−25% XP)

Make sure every opening parenthesis has a closing one.

Show solution (0 XP)
print('Hello')

Challenge 2 +50 XP

This code raises a ZeroDivisionError. Change the divisor from 0 to 2 to fix the bug.

python
  • Test 1 — expects "50.0\n"
Need a hint? (−25% XP)

Replace the 0 with a 2.

Show solution (0 XP)
score = 100
average = score / 2
print(average)