← Lesson 22

Lesson 23 · Python

Problem Solving & Debugging

~40 min

1. The Problem-Solving Mindset

Coding is not just writing syntax; it is solving problems. When faced with a complex task, never start typing code immediately. Follow this structured process:

  • Deconstruct: Break the large problem down into tiny, independent subproblems (e.g., instead of "build a game", start with "how do I generate one random number").
  • Write Pseudocode: Write the logical steps in plain, structured English comments inside your editor before translating them to Python.
  • Incremental Coding: Write 2–3 lines of code, run the script, check the output, and only move on when it works. Never write 100 lines at once.

2. Understanding Python Tracebacks

When your Python program crashes, it prints a **Traceback**. Do not panic or close the terminal! The traceback contains the exact diagnosis of the problem. Read it from the **bottom line up**:

  • SyntaxError: Bad formatting, missing colons, or mismatched quotes/parentheses.
  • NameError: Using a variable or function name that hasn't been defined (often due to spelling typos).
  • TypeError: Performing operations on invalid data types (e.g., trying to add a string to an integer).
  • IndexError: Trying to access an index position in a list that lies outside its boundaries.
# Sample Error trace:
Traceback (most recent call last):
  File "main.py", line 4, in 
    print("User age is: " + age)
TypeError: can only concatenate str (not "int") to str

3. Debugging Strategies

When code outputs incorrect results (logical errors) instead of crashing, use these methods:

  • Print Tracing: Insert print(f"DEBUG: variable is {var}") at intermediate steps to inspect state changes.
  • Dry Run: Read the code line by line, writing down the values of variables on a notepad as if you were the interpreter.

4. Common Beginner Mistakes

⚠️ Watch out for these mistakes:

  • Ignoring the Traceback line number: Changing code randomly instead of looking at the exact line number printed in the traceback.
  • Panicking at errors: Treating exceptions as failures. Errors are guides; they tell you exactly what line failed and why.
  • Writing too much code without testing: Coding for hours without running the script once, making it nearly impossible to locate where the bug was introduced.
Interactive Practice

Mini Practice: Debugging Challenge

A student wrote a script to compute averages, but it crashes. Locate the bugs (there are three: a typo, a syntax error, and a type mismatch), correct them in your editor, and verify the checklist.

0% complete
Check when done
Quiz

Test Your Understanding

1. How should you read a Python Traceback when a script crashes?

2. What error type is raised when you type `pint("Hello")` instead of `print("Hello")`?

3. What is the recommended strategy for writing code to avoid hard-to-find bugs?

🚀 Progression

Debugging is completed. Next, you'll learn how to track changes, write commit messages, and collaborate on GitHub using **Git Version Control**.