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


