Game Development Reference
In-Depth Information
14 - Caesar Cipher
in a way that is very similar to the isdigit() and isalpha() methods. isupper
() will return True if the string it is called on contains at least one uppercase letter and no
lowercase letters. islower() returns True if the string it is called on contains at least
one lowercase letter and no uppercase letters. Otherwise these methods return False . The
existence of non-letter characters like numbers and spaces does not affect the outcome.
Although strings that do not have any letters, including blank strings, will also return
False . Try typing the following into the interactive shell:
>>> 'HELLO'.isupper()
True
>>> 'hello'.isupper()
False
>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
>>> 'LOOK OUT BEHIND YOU!'.isupper()
True
>>> '42'.isupper()
False
>>> '42'.islower()
False
>>> ''.isupper()
False
>>> ''.islower()
False
>>>
How the Code Works: Lines 36 to 57
The process of encrypting (or decrypting) each letter is fairly simple. We want to apply
the same Python code to every letter character in the string, which is what the next several
lines of code do.
Encrypting or Decrypting Each Letter
36. if symbol.isupper():
37. if num > ord('Z'):
38. num -= 26
39. elif num < ord('A'):
40. num += 26
This code checks if the symbol is an uppercase letter. If so, there are two special cases
we need to worry about. What if symbol was 'Z' and key was 4 ? If that were the case,
 
Search WWH ::




Custom Search