When writing programs, it is unlikely that your code will work perfectly on the first attempt. Finding and fixing errors is called debugging. There are three main types of errors.

1. Syntax Errors

A syntax error means your code breaks the rules of the Python language. The program will not run at all.

Common syntax errors include:

Can you fix these?

Print("Hello")

Capital P — Python is case-sensitive. The correct version is:

print("Hello")
print("Hello world')

Mismatched quotes (double quote opening, single quote closing):

print("Hello world")
print("The answer is " + 42)

Cannot concatenate a string and an integer. Fix with:

print("The answer is " + str(42))

2. Runtime Errors

A runtime error occurs while the program is running. The syntax is valid, but something goes wrong during execution.

Common runtime errors:

Example

number = int(input("Enter a number: "))
result = 10 / number  # Runtime error if user enters 0!
print(result)
What happens if the user enters 0?

Python raises a ZeroDivisionError because you cannot divide by zero. The program crashes at that line.

3. Logical Errors

A logical error means the program runs without crashing, but produces the wrong result. These are the hardest to find because Python does not report them.

Example

base = float(input("Enter the base: "))
height = float(input("Enter the height: "))
perimeter = base + height + base + base  # Wrong formula!
print("The perimeter is", perimeter)
What is wrong with this code?

The formula for the perimeter of a rectangle should be 2 * (base + height), but the code adds base three times instead. The corrected line is:

perimeter = 2 * (base + height)

Debugging Tips

  1. Read the error message carefully — it tells you the line number and type of error
  2. Check your spelling — Python is case-sensitive
  3. Check your brackets and quotes — make sure they match
  4. Print intermediate values — use print() to check what your variables contain
  5. Test with simple values — use inputs where you know the expected output