Game Development Reference
In-Depth Information
the value of num here would be the character '^' (The ordinal of '^' is 94 ). But ^ isn't
a letter at all. We wanted the ciphertext to "wrap around" to the beginning of the alphabet.
The way we can do this is to check if key has a value larger than the largest possible
letter's ASCII value (which is a capital "Z"). If so, then we want to subtract 26 (because
there are 26 letters in total) from num . After doing this, the value of num is 68 , which is
the ASCII value for 'D' .
41. elif symbol.islower():
42. if num > ord('z'):
43. num -= 26
44. elif num < ord('a'):
45. num += 26
If the symbol is a lowercase letter, the program runs code that is very similar to lines 36
through 40. The only difference is that we use ord('z') and ord('a') instead of ord
('Z') and ord('A') .
If we were in decrypting mode, then key would be negative. Then we would have the
special case where num -= 26 might be less than the smallest possible value (which is
ord('A') , that is, 65 ). If this is the case, we want to add 26 to num to have it "wrap
around".
47. translated += chr(num)
48. else:
49. translated += symbol
The translated string will be appended with the encrypted/decrypted character. If
the symbol was not an uppercase or lowercase letter, then the else-block on line 48 would
have executed instead. All the code in the else-block does is append the original symbol to
the translated string. This means that spaces, numbers, punctuation marks, and other
characters will not be encrypted (or decrypted).
50. return translated
The last line in the getTranslatedMessage() function returns the translated
string.
The Start of the Program
52. mode = getMode()
53. message = getMessage()
54. key = getKey()
Search WWH ::




Custom Search