Practicing with the VS Code Debugger

Practicing with the VS Code Debugger#

You’ve been asked by a colleague to help them figure out why they’re having issues with a package they wrote. The packages designed to compare two sentences, figure out how many words they have in common, and then return the word that (a) appears in both sentences, and (b) when more than one word appears in most sentences, identify the word in both sentences with the most total occurrences.

So for example, if our sentences were:

“Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun.”

  • “Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.”

The result would be the word “of” (it appears in both, and appears 3 times in total).

Exercise 1:#

Download your colleague’s script here and familiarize yourself with its contents.

Note that when you run this file from the command line (e.g. navigate to where it’s located and run python debugger.py), the first thing executed are the three test cases under if __name__ == "__main__":. As you can see, these all call word_counter, so you should start your examination there.

One trick, for those who aren’t familiar with string manipulation: You can subset a string just the way you would a list. So for example if I have a string:

my_string = "My name is Nick"

I can shorten it to my name by subsetting:

my_string[11:]
'Nick'

Exercise 2:#

Run the script from the command line. What happens? Did all three tests pass?

Exercise 3:#

What type of error did you just encounter—a logical error or a syntax error?

Exercise 4:#

Now run debugger.py in VS Code’s Python Debugger. Try to figure out the problem you saw when you ran your code at the command line.

Exercise 5:#

Once you’ve fixed that bug, run your script from the command line again. Did the code run without problems? Did all the tests turn out as expected?

Exercise 6:#

Based on your answers to Exercise 5, which kind of bug are you encountering: a syntax error or a logical error?

Exercise 7:#

Using the debugger, try and track down the source of the problem you’ve now found.