liveBook · Manning

Bug

As explained in Learn AI-Assisted Python Programming, Second Edition

A software bug is a defect in a program that can cause it to produce incorrect behavior or fail under particular conditions.

A bug is a mistake in a program that makes it do the wrong thing or fail under particular conditions. Bugs are a normal part of programming, including AI-assisted programming, and debugging means finding and removing them. section 1.4 section 8.0

Recognize two primary kinds of bugs:

  • Syntax errors violate Python’s writing rules, such as omitting the colon after a for-loop header. Python reports them when it cannot interpret the code. section 8.1
  • Logical errors run but produce the wrong result because the implementation does not match the intended behavior. section 8.1

Find bugs by testing, reading the code, changing prompts, trying alternative Copilot suggestions, or asking Copilot for a fix. When those approaches do not reveal the cause, inspect the program’s behavior with targeted print statements or the VS Code debugger. Work iteratively: compare what the program does with what you expected, narrow the faulty step, then fix and retest it. section 8.0 section 8.2 section 8.2.1

Counting an exact word

A function intended to count exactly "Dan" returns 2 for ["Dan", "danger", "Leo"] because it checks whether "dan" appears anywhere in each word. Printing the word when count increases exposes the mistake: both "Dan" and "danger" are counted. Replacing the condition with word == "Dan" fixes the bug. section 8.2.1

Testing boundary behavior

A count_between function using number >= x and number <= y returns 5 for numbers 1 through 9 with bounds 3 and 7. If “between” excludes the boundaries, changing the comparisons to number > x and number < y produces the intended count of 3. section 8.3.4

Listing 8.1 Incorrect function for counting matching words
def count_words(words):
    count = 0
    for word in words:
        if "dan" in word.lower():    #1
            count += 1
    return count

Inspect the incorrect word-counting function to locate the condition that counts partial matches. section 8.2.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines syntax and logical errors, demonstrates print-based diagnosis, and applies boundary testing, while deferring extensive low-level implementation detail.
Examples
Demonstrates word matching with Dan and danger, boundary counting from 3 to 7, targeted print statements, and corrected Python conditions.
What it assumes
Assumes basic ability to read and run Python code while teaching bug terminology, testing habits, and debugging from small examples.
What you can do after
Diagnose and fix bugs by testing functions, inspecting code behavior, using targeted output, prompting Copilot, and applying debugger tools.
How it connects
Connects bugs with debugging, testing, syntax errors, logical errors, problem decomposition, Python, GitHub Copilot, and the VS Code debugger.

Also explained in

Compare how 3 books explain bug →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.