Debugging#

1. Identifying bugs in code#

The following stripped_reversed_lowercase function contains at least one bug. You can see this by running the code in the cell below which tests the functionality of the stripped_reversed_lowercase function.

Set trace at the beginning of stripped_reversed_lowercase and use debugger to solve the bug(s). Execute the code line by line and print variables used in the function to understand what’s going wrong.

def stripped_reversed_lowercase(original):
    # Set a breakpoint here and start debugging
    stripped = original.lstrip()
    reversed = ' '.join(reversed(stripped))
    reversed.lower()
    return reversed
# Let's verify it works
original = ' \n Original String '
result = stripped_reversed_lowercase(original)
assert result == 'gnirts lanigiro'
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[2], line 3
      1 # Let's verify it works
      2 original = ' \n Original String '
----> 3 result = stripped_reversed_lowercase(original)
      4 assert result == 'gnirts lanigiro'

Cell In[1], line 4, in stripped_reversed_lowercase(original)
      1 def stripped_reversed_lowercase(original):
      2     # Set a breakpoint here and start debugging
      3     stripped = original.lstrip()
----> 4     reversed = ' '.join(reversed(stripped))
      5     reversed.lower()
      6     return reversed

UnboundLocalError: local variable 'reversed' referenced before assignment