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 and return them in alphabetical order (with capital letters first).
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 ['of', 'the'].
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 ['Batman', 'always', 'be', 'should', 'you'].
Setup#
Unfold and copy this script into a new .py file and save it somewhere on your computer:
"""Debugger exercise."""
def split_string(string):
"""Given a string, split it into a list of 'words'."""
# Remove last period
string = string[:-1]
# Split
words = string.split()
return words
def get_repeats(sentence_as_list):
unique_words = []
repeated_words = []
for i in range(1, len(sentence_as_list)):
# If we haven't see it before, put in
# unique words list.
word = sentence_as_list[i]
if word not in unique_words:
unique_words.apend(word)
# If we have seen it before (it's in
# the unique words list),
# it is a repeat and we put it in
# repeated words if it isn't already there.
elif word in unique_words and word not in repeated_words:
repeated_words.append(word)
# Sort list alphabetically
# (with capital letters sorting ahead of lower case)
repeated_words = sorted(repeated_words)
return repeated_words
def get_repeated_words(sentence):
"""Get a list that contains one copy of each repeated word in sentence."""
# Separate words in sentence
sentence_as_list = split_string(sentence)
# Figure out what is repeated
repeated_words = get_repeats(sentence_as_list)
return repeated_words
def main():
# Test Case 0
print("Test Case 0:")
sentence = (
"you should always be yourself unless you can "
"be Batman then you should always be Batman."
)
print(f"The sentence is: {sentence}")
print("The result should be ['Batman', 'always', 'be', 'should', 'you']")
test_word = get_repeated_words(sentence)
print(f"Result is: {test_word}")
# Test Case 1
print("Test Case 1:")
sentence = (
"far out in the uncharted backwaters of the "
"unfashionable far end of the western spiral arm of "
"the Galaxy lies a small unregarded yellow sun."
)
print(f"The sentence is: {sentence}")
print("The result should be ['far', 'of', 'the']")
test_words = get_repeated_words(sentence)
print(f"Result is: {test_words}")
main()
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.