[Tutor] implementing rot 13 problems

Danny Yoo dyoo at hashcollision.org
Tue Mar 12 20:12:26 CET 2013


> ##################################
> ## rot1: char -> char
> ## Rotates a single character.
> def rot1(char):
>     if char.isupper() or char.islower():
>         test = 'M' if char.isupper() else 'm'
>     if char <= test:
>         return chr(ord(char) + 13)
>     else:
>         return chr(ord(char) - 13)


Also note that I've deliberately left alone a bug in rot1(), to make
it easier to show a flaw in the original code that you'll want to fix.
 The bug is in the definition and use of 'test'.  Not only is the
variable name somewhat undescriptive, but it's only being defined in
only one branch of the conditional.  That's a "bad" code smell, and in
this case, you'll find an issue here when trying to rotate a
non-alphabetic character.


More information about the Tutor mailing list