Learn Before
Code
Mocking Example
##Say that I am creating a function meant to ##shift every character in a text document up ##one character, ie. 'b' would turn to 'c'. text=moby_dick.txt shift=1 def shift_text(text, shift): shifted='' for ch in text: shifted+=shifted.join(chr(97+((ord(ch)-96+shift)%26))) else: shifted+=ch return shifted ##For some reason, this function doesn't work ##when I run it. To debug this, I decide to just ##manipulate the string 'How are you?'. That ##would look like this: text='How are you?' shift=1 def shift_text(text, shift): shifted='' for ch in text: shifted+=shifted.join(chr(97+((ord(ch)-96+shift)%26))) else: shifted+=ch return shifted ##This lets me see that '?' characters are being ##changed, which is not something that I want. ##Thus, I now know I need to exclude non-letter ##characters: def shift_text(text, shift): shifted='' for ch in text: if 'a'<=ch and 'z'>=ch: shifted+=shifted.join(chr(97+((ord(ch)-96+shift)%26))) else: shifted+=ch return shifted ##Now my code works, through the use of mocking.
0
0
Updated 2021-06-10
Tags
Python Programming Language
Data Science