ZAMYLA: Let's slither
around the Caesar cipher.
Caesar in Python is going to have the
same to-dos as the Caesar problem in C.
We're going to get the key from
the user, get the plaintext,
encipher that plaintext, and then
finally, print that cipher text back
to the user.
The usage for the
Caesar problem in Python
is going to be very
similar to the usage in C
whereby we pass in the
key in the command line
and then prompt the user
later on for the plaintext.
So how do we get the key?
Well, passed in the command
line, we access that in C
using the variable argv.
And because it's an array,
we access that at index 1.
In Python, it's going to be the same.
But in order to access
argv, we're going to have
to add in system, or sys, before that.
Then argv is a string.
So we need to cast that to an integer.
In C, we used the atoi function.
And in Python, we're going to add
int before that and then the variable
in brackets.
And there you have
your key as an integer.
So the next step is
to get the plaintext.
In C, we provided you within the
CS50 library the get_string function.
Similarly, we also provide you with
the get_string function in Python.
You just have to import the CS50 module.
In this problem, we'll want to
prompt the user for their plaintext.
And we'll also want to
print back their ciphertext.
We'll use the print function for this.
Now, in Python, it
automatically prints something
with a new line unless
otherwise specified.
So if you want to print
something without a new line,
then you'll have to add in a comma
after the string that you're printing
and put an n equals with
two empty quotation marks.
So now that you have that,
let's talk about enciphering.
The algorithm isn't going to change.
For each character in the plaintext
string, if it's alphabetic,
then we'll want to shift that character
by the key, preserving the case,
whether uppercase or lowercase.
Say I wanted to print out every
letter of my name in a new line.
Then if I have a string containing
my name, Zamyla, then in a for loop,
I could do as follows--
for c in name print c, where c
is the iterator variable where
each character is stored.
So now that we have that, we
need to remember that we only
shift alphabetic characters.
And we want to preserve the case.
In C, we used isalpha,
isupper, and islower functions.
And we have those, as well, in Python.
Just make sure to check how to
properly use those functions.
Now we come to the Caesar formula, where
we add the key to our plaintext letter
and then modulo that result by 26.
Why the modulo?
Well, remember that we need
to do an alphabet wraparound.
So if I have the letter Y, for
instance, and add the key of 2,
then I want that to wrap
around back to the letter A.
But when we calculated this
using our ASCII values, then
we actually didn't get the right value.
Rather, we found that we had to use
our alphabetical index, referring to Y
as 24 in order for that all to work.
But then once we got there, we had
to cast from the alphabetical index
back to our ASCII index in order to
print and display the right letter.
So how do we go back and forth between
ASCII values and alphabetical indices?
Well, I bet you that in Python, you'll
find these two functions quite helpful.
Once you figure that
out, you can successfully
slither around your cipher.
My name is Zamyla, and this was Caesar.
