The Visual Studio Code debugger helps you understand program behavior by pausing execution, inspecting values, and moving through relevant lines one at a time. section 8.2.2
A practical debugging workflow is:
- Set a breakpoint by clicking to the left of a relevant line. section 8.2.2
- Choose Run > Start Debugging. section 8.2.2
- Use step-by-step execution to inspect variable values and follow the code path. section 8.2.2
Tracing an incorrect word counter
Debug a count_words function with the inputs ["Dan", "danger", "Leo"]. Pause at the function, step through the loop and condition, and watch count change as each word is processed. section 8.2.2
Listing 8.4 Incorrect count_words function for debugging
def count_words(words):
count = 0
for word in words:
if "dan" in word.lower():
count += 1
return count
words = ["Dan", "danger", "Leo"] #1
print(count_words(words)) #1
Inspect the intentionally incorrect count_words example used for breakpoint and line-by-line debugging. section 8.2.2
