Hacking Secret Ciphers with Python

(Ann) #1

246 http://inventwithpython.com/hacking


Email questions to the author: [email protected]





'HELLO WORLD 123'.isupper()
True
'hELLO'.isupper()
False
'hELLO'.islower()
False
'hello'.islower()
True
' 123 '.isupper()
False
''.islower()
False





simpleSubCipher.py
59. if symbol.isupper():
60. translated += charsB[symIndex].upper()
61. else:
62. translated += charsB[symIndex].lower()


If symbol is an uppercase letter, than we want to concatenate the uppercase version of the
character at charsB[symIndex] to translated. Otherwise we will concatenate the
lowercase version of the character at charsB[symIndex] to translated.


If symbol was a number or punctuation mark like '5' or '?', then the condition on line 5 9
would be False (since those strings don’t have at least one uppercase letter in them) and line 62
would have been executed. In this case, line 62’s lower() method call would have no effect on
the string since it has no letters in it. Try typing the following into the interactive shell:





'5'.lower()
'5'
'?'.lower()
'?'





So line 62 in the else block takes care of translating any lowercase characters and non-letter
characters.


simpleSubCipher.py
63. else:
64. # symbol is not in LETTERS, just add it
65. translated += symbol

Free download pdf