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 script they wrote. The package is designed to figure out what words in a sentence are repeated.
So for example, if our sentence 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.
The script should return ['the', 'of']
.
Or if the sentence were:
You should always be yourself unless you can be Batman then you should always be Batman.
The script should return ['be', 'you', 'should', 'always', 'Batman']
.
Exercise 1:#
Download your colleague’s script here and familiarize yourself with its contents.
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. What happens? Did it run? Did both tests pass?
Exercise 3:#
What type of error did you just encounter—a logical error or a syntax error?
Exercise 4:#
To address this error, run the script in VS Code’s Python Debugger. Try to figure out the problem you saw when you ran your code.
(Admittedly, the first bug is one you might be able to solve without the debugger, but let’s try it!)
Exercise 5:#
Once you’ve fixed that bug, run your script 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.