Computer Programming 1 (Python) • Debugging, Testing & Code Quality

Error Types — I can distinguish syntax errors, runtime errors, and logic errors and give examples.

Learn to identify the three fundamental categories of programming errors and fix them efficiently.

A Python coding terminal with a program flow and laptop.
Inside this lesson 10 sections 5 questions 5 figures

SECTION 1

Historical Context & Motivation

Every programmer, from absolute beginners to seasoned professionals, encounters errors — sometimes called bugs — in their code. The word "bug" itself has a storied history in computing, and the practice of finding and fixing errors has evolved alongside programming languages themselves. Understanding how the computing community learned to classify errors helps you appreciate why we still sort them into three distinct categories today.

1947 • The First Literal "Bug"

Grace Hopper's team at Harvard found a moth trapped inside the Mark II computer, coining the popular use of "bug" for a computer malfunction. The term "debugging" soon followed.

1950s • Early Compilers & Syntax Checking

As programming languages like FORTRAN emerged, compilers began automatically checking code structure before execution, creating the first formal category of errors: syntax errors.

1960s • Runtime Errors Gain a Name

Operating systems started catching crashes during program execution — such as division by zero or memory violations — and reporting them as runtime errors distinct from compile-time issues.

1972 • Logic Errors & Software Engineering

The growing software crisis highlighted that programs could run without crashing yet still produce wrong results. The concept of logic errors became a central concern in the emerging discipline of software engineering.

1991 • Python's Friendly Error Messages

Guido van Rossum released Python, which was designed with readable, descriptive error messages — called tracebacks — making it easier for beginners to identify and fix syntax and runtime errors.

Over decades of programming, the community settled on three broad categories of errors: those the language catches before running your code, those that crash during execution, and those that silently produce incorrect output. The key question this lesson addresses is: how do you tell them apart, and what do you do about each one?

SECTION 2

Core Principles & Definitions

Before you can fix a problem, you need to diagnose it correctly. Just as a doctor classifies symptoms before prescribing treatment, a programmer classifies errors before debugging. Python errors fall into three fundamental types, each occurring at a different stage of your program's lifecycle.

1

Syntax Error

A violation of the language's grammar rules. Python detects these before your program runs. Common causes include missing colons, unmatched parentheses, and misspelled keywords.
2

Runtime Error

An error that occurs during program execution, causing the program to crash. The syntax is valid, but an operation fails — for example, dividing by zero or accessing a list index that doesn't exist.
3

Logic Error

The program runs without crashing but produces incorrect results. The code is syntactically valid and doesn't crash, but the algorithm or formula is wrong. These are the hardest to find because Python gives no error message.
✦

KEY TAKEAWAY

Think of writing code like baking a cake. A syntax error is like writing "preheat the oevn" — the recipe is unreadable and you can't even start. A runtime error is like discovering mid-recipe that you're out of eggs — the process halts. A logic error is like accidentally using salt instead of sugar — the cake comes out fine, but it tastes terrible. The result is wrong even though you followed every step.

SECTION 3

Visual Explanation

The Lifecycle of a Python Program & Where Errors OccurYou Write Codemy_program.pyPython Parses Code(Checks grammar rules)Python Executes Code(Runs line by line)⛔ SYNTAX ERRORCaught BEFORE runningprint("hello"^ Missing closing )💥 RUNTIME ERRORCrashes DURING runningx = 10 / 0ZeroDivisionError🤔 LOGIC ERRORWrong answer, NO crashavg = a + b / 2Should be (a + b) / 2Program finishes but...
This diagram shows the lifecycle of a Python program from writing code through parsing and execution. Syntax errors are caught at the parsing stage, runtime errors crash during execution, and logic errors slip through undetected because the program runs to completion with wrong results.

As the diagram illustrates, each error type corresponds to a different phase of your program's life. When you press "Run," Python first reads (parses) your entire file to check that the grammar is valid. If it finds a broken rule, it stops immediately with a SyntaxError — your code never even starts executing. If the grammar checks out, Python begins executing your code line by line. An instruction that is grammatically correct can still fail at this stage, producing a runtime error like ZeroDivisionError or IndexError. Finally, if every line runs without crashing, you might still get the wrong answer due to a logic error — and Python won't warn you at all.

SECTION 4

How Python Detects Each Error Type

Syntax Errors: The Parser's Job

When you run a Python file, the interpreter first converts your source code into an internal representation called an Abstract Syntax Tree (AST). During this process, the parser checks every line against Python's grammar rules. If it encounters something it doesn't understand — like a missing colon after an if statement or mismatched quotes — it raises a SyntaxError and points to the approximate location of the mistake with a caret (^) symbol.

ℹ

Common Syntax Errors in Python

Missing colons after if, for, while, and def statements; unmatched parentheses, brackets, or quotes; incorrect indentation; using = instead of == in comparisons; and misspelling keywords like retrun instead of return.

Runtime Errors: The Executor's Problem

Once the code passes the parser, Python begins running it line by line. A runtime error (also called an exception) occurs when an instruction is grammatically correct but asks the computer to do something impossible or undefined. Python names these errors precisely: a ZeroDivisionError means you tried to divide by zero, a TypeError means you mixed incompatible data types, and a NameError means you used a variable that hasn't been defined. The program halts at the exact line where the error occurs and prints a traceback showing the chain of function calls that led to the crash.

Logic Errors: The Programmer's Blind Spot

A logic error is the sneakiest type because Python provides absolutely no error message. The code is valid, runs to completion, and produces output — but that output is wrong. These errors stem from mistakes in your algorithm or reasoning, such as using the wrong formula, placing a statement inside a loop when it should be outside, or using > when you meant >=. To catch logic errors, you must test your program with known inputs and compare the actual output to the expected output.

⚠

Why Logic Errors Are the Hardest to Fix

Syntax errors and runtime errors give you an error message and a line number. Logic errors give you nothing — the program looks like it's working perfectly. The only clue is that your output doesn't match what you expected. This is why testing with known inputs and expected outputs is one of the most important habits a programmer can develop.

SECTION 5

Detailed Error Classification with Examples

Now that you understand the three categories conceptually, let's look at specific Python error names and code examples for each one. The table below maps common error names to their type and provides a quick code snippet showing what triggers each one.

Error TypePython Error NameExample CodeWhat Went Wrong
SyntaxSyntaxErrorif x == 5Missing colon at end of if statement
SyntaxIndentationErrordef greet():\nprint('hi')Body of function not indented
RuntimeZeroDivisionErrorresult = 10 / 0Cannot divide by zero
RuntimeNameErrorprint(mesage)Variable 'mesage' was never defined (typo)
RuntimeIndexErrorlst = [1,2]; lst[5]Index 5 doesn't exist in a 2-element list
RuntimeTypeError'age: ' + 17Cannot concatenate a string and an integer
Logic(No error name)avg = a + b / 2Should be (a + b) / 2 — order of operations
Logic(No error name)if score > 90:Should be >= 90 if 90 qualifies for an A

Common Python errors organized by type

Error Comparison: Symptoms & DetectionSyntax ErrorWhen detected?Before executionError message?✅ Yes — with caret (^)Program runs?❌ NoDifficulty to fix:Easy ★☆☆Example:for i in range(5) ^ missing :Fix: Add the colonRuntime ErrorWhen detected?During executionError message?✅ Yes — tracebackProgram runs?⚠️ Partially, then crashesDifficulty to fix:Medium ★★☆Example:nums = [1, 2, 3]print(nums[10])Fix: Check list lengthLogic ErrorWhen detected?Only when YOU check outputError message?❌ NoProgram runs?✅ Yes — fullyDifficulty to fix:Hard ★★★Example:total = price * 0.8# Meant 0.08 tax rateFix: Verify your formula
Side-by-side comparison of the three error types showing when they're detected, whether Python gives an error message, and their relative difficulty to fix.

Notice the pattern in the comparison above: the later an error is caught, the harder it is to fix. Syntax errors are caught immediately with a clear message and caret pointing to the problem. Runtime errors give you a traceback, but the real cause might not be on the exact line that crashed. Logic errors provide no error message at all, meaning you have to rely on testing and careful reasoning to track them down.

SECTION 6

Worked Example: Diagnosing Errors in a Program

Let's walk through a short Python program that is supposed to calculate the average of three test scores. The original code has one error of each type. We'll identify, classify, and fix each one.

Finding and Fixing All Three Error Types

  1. 1

    Step 1 — Read the Original Code

    Here is the buggy program: score1 = 85 score2 = 90 score3 = int(input("Enter score 3: ") average = score1 + score2 + score3 / 3 print("Average: " + average) Our goal is to find all three error types before running the program.
  2. 2

    Step 2 — Identify the Syntax Error

    Look at line 3: score3 = int(input("Enter score 3: "). Count the parentheses — there are three opening parentheses but only two closing ones. Python's parser will catch this and report a SyntaxError before the program starts.
    Fix: Add the missing closing parenthesis → score3 = int(input("Enter score 3: "))
  3. 3

    Step 3 — Identify the Runtime Error

    After fixing the syntax error, the program will run. Look at line 5: print("Average: " + average). The variable average is a number (float or int), and you cannot concatenate a string with a number using + in Python. This will raise a TypeError when the program reaches that line.
    Fix: Convert the number to a string → print("Average: " + str(average))
  4. 4

    Step 4 — Identify the Logic Error

    After fixing both previous errors, the program runs and prints a result. But look at line 4: average = score1 + score2 + score3 / 3. Due to Python's order of operations, score3 / 3 is calculated first, and then added to score1 and score2. If all three scores are 90, this gives 90 + 90 + 30 = 210 instead of the correct average of 90. Python won't complain — it just gives the wrong answer.
    Fix: Add parentheses → average = (score1 + score2 + score3) / 3
  5. 5

    Step 5 — Verify the Corrected Program

    The corrected program: score1 = 85 score2 = 90 score3 = int(input("Enter score 3: ")) average = (score1 + score2 + score3) / 3 print("Average: " + str(average)) Test it with score3 = 95: the average should be (85 + 90 + 95) / 3 = 90.0. If the program outputs 90.0, all three bugs are fixed.
    Expected output: Average: 90.0 ✅

SECTION 7

Comparing Error Types: Strengths & Strategies

Each error type requires a different debugging strategy. Knowing which approach to use saves time and reduces frustration. The table below summarizes what to look for and how to fix each type.

FeatureSyntax ErrorRuntime ErrorLogic Error
Detected byPython's parserPython's executorYou (the programmer)
WhenBefore any code runsWhen the faulty line executesWhen you check output
Error message?Yes — SyntaxError with caretYes — named exception with tracebackNo message at all
Fix strategyRead the error message, check the indicated line and the line above itRead the traceback from bottom to top, add input validation or type conversionTest with known inputs, use print statements or a debugger, trace through code by hand
PreventionUse an IDE with syntax highlighting and lintingValidate user input, use try/except blocksPlan your algorithm before coding, write test cases

Debugging strategy comparison for each error type

✦

KEY TAKEAWAY

Think of debugging like troubleshooting a car. A syntax error is like turning the key and the car won't start — the dashboard immediately tells you what's wrong. A runtime error is like driving down the road and hitting a pothole that blows a tire — you were moving, but something specific stopped you. A logic error is like driving smoothly to the wrong destination — the car works perfectly, but you took a wrong turn. The best debugging strategy depends on which scenario you're dealing with.

SECTION 8

Connection to Advanced Error Handling

Understanding the three error types is just the beginning. As you advance in Python, you'll learn techniques specifically designed to handle and even prevent each type. The table below previews how beginners and more advanced programmers deal with errors differently.

ConceptWhat You Learn NowWhat Comes Next
Syntax ErrorsRead the SyntaxError message and fix the typoUse linters (like pylint or flake8) that catch errors in real time as you type
Runtime ErrorsRead the traceback and fix the crashUse try/except blocks to gracefully handle errors, write custom exception classes
Logic ErrorsUse print statements to check intermediate valuesWrite unit tests with frameworks like unittest or pytest to automatically verify correctness
Overall ApproachFix errors one at a time as they appearPractice test-driven development (TDD) — write tests before code to prevent logic errors

From beginner error fixing to professional error handling

In more advanced courses, you'll discover that Python's exception handling system with try, except, finally, and raise gives you powerful tools to anticipate runtime errors and respond gracefully instead of crashing. You'll also learn to write automated tests — small programs that check whether your main program produces the right output for dozens or hundreds of input cases. These skills build directly on the error classification you're learning today.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

A student runs their Python program and sees no error message, but the program outputs 50 when the correct answer should be 25. What type of error is this, and why doesn't Python catch it?
Show answer
This is a logic error. Python does not catch logic errors because the code is syntactically correct and no illegal operations occur during execution. The program runs to completion without crashing, but the algorithm or formula in the code is wrong, causing it to produce an incorrect result. Python has no way to know what the 'right' answer is — only the programmer can determine that by comparing the output to the expected result.

Question 2: BASIC CALCULATION

Classify each of the following as a syntax error, runtime error, or logic error: (a) print("Hello World" (b) x = int("abc") (c) A program that should calculate the area of a circle using area = 2 * 3.14 * r instead of area = 3.14 * r ** 2
Show answer
(a) Syntax error — the closing parenthesis is missing, so Python's parser cannot understand the line and will raise a SyntaxError before running any code. (b) Runtime error — the syntax is valid, but when Python tries to convert the string "abc" to an integer during execution, it raises a ValueError because "abc" is not a number. (c) Logic error — the formula 2πr calculates circumference, not area. The code runs without crashing but produces the wrong value. The correct formula is πr².

Question 3: INTERMEDIATE

The following program is supposed to ask the user for a number and print whether it is positive, negative, or zero. Identify all errors, classify each one, and provide corrected code. num = input("Enter a number: ") if num > 0 print("Positive") elif num = 0: print("Zero") else: print("Negative")
Show answer
There are three errors. First, line 2 is missing a colon after the condition — this is a syntax error; fix it by writing if num > 0:. Second, line 4 uses = (assignment) instead of == (comparison) — also a syntax error; fix it to elif num == 0:. Third, line 1 stores the input as a string (since input() always returns a string), so comparing it with > to an integer will cause a runtime TypeError. Fix it with num = int(input("Enter a number: ")) or num = float(input("Enter a number: ")).

Question 4: APPLIED

A teacher writes a program to calculate the class average from a list of grades. The program runs without error messages but consistently reports an average that is too low. The code is: grades = [88, 92, 75, 95, 80] total = 0 for g in grades: total = total + g average = total / len(grades) print(average) Explain what type of error this is, why the result is wrong, and how to fix it.
Show answer
This is a logic error. The line average = total / len(grades) is indented inside the for loop, so the average is recalculated on every iteration while the total is still being built up. By coincidence the final iteration does compute the correct value here (because total equals the full sum on the last pass), but the indentation reveals a misunderstanding. In more complex programs, placing the averaging step inside the loop would waste computation and could lead to using a premature average. The fix is to un-indent the average line so it executes only after the loop finishes: move average = total / len(grades) to the same indentation level as print(average). Actually, in this specific example the printed answer happens to be correct (86.0), but the program is logically inefficient and would produce wrong intermediate averages if used inside the loop for other purposes.

Question 5: CRITICAL THINKING

Is it possible for a single line of code to be free of syntax errors, free of runtime errors in some cases but cause a runtime error in other cases? Give a concrete Python example and explain the conditions that determine whether the error occurs. Then explain how this fact connects to the importance of testing with multiple inputs.
Show answer
Yes, absolutely. Consider the line result = 100 / x. This has no syntax errors. If x = 5, it runs perfectly and produces 20.0. If x = 0, it crashes with a ZeroDivisionError. The exact same line of code behaves differently depending on the value of x at the time of execution. This demonstrates that runtime errors are context-dependent — they depend on the data flowing through the program, not just the code itself. This is precisely why testing with multiple inputs is essential: a program that works for one test case might crash or give wrong answers for another. Professional developers use edge cases (like zero, negative numbers, empty strings, or very large values) specifically to hunt for runtime errors that only appear under unusual conditions.

SUMMARY

Lesson Summary

Summary

Python programs can contain three types of errors. Syntax errors violate the language's grammar rules and are caught by the parser before any code runs — they produce a SyntaxError message with a caret pointing to the problem. Runtime errors (exceptions) occur during execution when a valid instruction encounters an impossible operation, such as dividing by zero or accessing a nonexistent list index. Python names these precisely (ZeroDivisionError, TypeError, IndexError) and provides a traceback showing the chain of calls.

Logic errors are the trickiest: the program runs without crashing but produces incorrect results because the algorithm or formula is wrong. Python provides no error message for logic errors, so the only way to detect them is to test with known inputs and compare actual output to expected output. Remember: the later an error is caught in the program lifecycle, the harder it is to fix. Building the habit of reading error messages carefully, using print() statements for debugging, and testing with multiple inputs will make you a much more effective programmer.

Varsity Tutors • Computer Programming 1 (Python) • Error Types — I can distinguish syntax errors, runtime errors, and logic errors and give examples.