Game Development Reference
In-Depth Information
one symbol in the plaintext with one (and only one) symbol in the ciphertext. So if a "G"
was substituted with "Z" in the cipher, every single "G" in the plaintext would be replaced
with (and only with) a "Z".
MAX_KEY_SIZE
is a variable that stores the integer
26
in it.
MAX_KEY_SIZE
reminds
us that in this program, the key used in our cipher should be between 1 and 26.
Deciding to Encrypt or Decrypt
5. def getMode():
6. while True:
7. print('Do you wish to encrypt or decrypt a
message?')
8. mode = input().lower()
9. if mode in 'encrypt e decrypt d'.split():
10. return mode
11. else:
12. print('Enter either "encrypt" or "e" or
"decrypt" or "d".')
The
getMode()
function will let the user type in if they want to encrypt or decrypt the
message. The return value of
input()
(which then has the
lower()
method called on it,
which returns the lowercase version of the string) is stored in
mode
. The
if
statement's
condition checks if the string stored in
mode
exists in the list returned by
'encrypt e
decrypt d'.split()
. This list is
['encrypt', 'e', 'decrypt', 'd']
, but
it is easier for the programmer to just type in
'encrypt e decrypt d'.split()
and not type in all those quotes and commas. But you can use whatever is easiest for you;
they both evaluate to the same list value.
This function will return the first character in
mode
as long as
mode
is equal to
'encrypt'
,
'e'
,
'decrypt'
, or
'd'
. This means that
getMode()
will return the
string
'e'
or the string
'd'
.
Getting the Message from the Player
14. def getMessage():
15. print('Enter your message:')
16. return input()
The
getMessage()
function simply gets the message to encrypt or decrypt from the
user and uses this string as its return value.