All right. It's finally time to tackle our final project,
the Caesar Cipher. As I mentioned before,
Caesar Cipher is this way of encoding text that,
um, was seen as early as during the times of Julius Caesar.
So when he had these top secret military messages,
what he would do is he would shift each letter of the alphabet by a certain
predetermined amount. I want to show you quickly how this works.
So we've got the alphabet here and let's say we wanted to encode the letter E. So
now we can line up the alphabet with a new set of alphabet.
And at the moment,
the shift is zero because the top alphabet and the bottom alphabet are lined up
with a zero difference. Now, let's say that we had a shift of one. Well,
then the alphabet moves to the left and A becomes B, B become C et cetera.
And we can keep going until we get to the amount of shift that we wanted.
So let's say we're going to encode all of our text with the shift of three.
Well, then E becomes H, F becomes I, G becomes J et cetera,
and so on and so forth.
And they've actually discovered these artifacts from ages ago
where people have created these sort of dials.
And by simply rotating the dial to a certain amount of shift,
then you can line up the letters with each other.
So A becomes N and Z becomes O and so on and so forth.
And this is a way that people actually encoded top secret messages.
By the end of this day,
you will have also built a digital form of the Caesar cipher.
And all you have to do is to type encode to start encoding a message.
Let's say something like Hello World.
And once we hit enter, we get to type the shift number,
which I'm just gonna choose a random one,
and it gives us the encoded result.
So now if I take this encoded results and I go ahead and type yes,
to restart my program,
and I'm going to use the decode function to decode my message.
Now, when I use the same shift number,
then I should be able to get back the decoded result.
So if there's people monitoring your phone, or you're trying to throw a message to
your friend in a paper ball,
then this is an easy way to ensure that if your message was intercepted then it
won't be understood by the other person. Once you're ready,
let's get started by heading over to repl.it/@appbrewery/caesar
-cipher-1-start. And this contains the starting code for this project
and this is part one of four.
So here I've already saved you the laborious task of typing out all the letters
in the alphabet and they're stores in a single list.
Now I've also got three inputs.
So this is what the user is going to type in.
They're gonna type in encode or decode,
and that's gonna be saved to the direction. Tey're going to type them message
which is going to be changed to lower case
and this is going to be saved in text, and finally,
they're going to input a shift number
which is going to be converted to an integer and saved inside this variable
called shift. So if you want to, have a run of the program as it is right now,
so that you fully understand what's actually going on before you get started.
And remember, don't change any of the code above,
but instead tackle the to-dos one by one.
The first step is to create a function called encrypt
that's going to take this text that the user typed in and the shift that they
typed in as inputs.
And this is going to use what you learned about functions with inputs,
from today's lessons.
And then once you're done with that, step 2 is to go inside the encrypt
function that you just created and shift each letter of the text forwards by the
shift amount. So, again, as we saw before a shift of one,
a shifter of two, a shift of three,
basically just moves the characters forward
so that each letter of the alphabet becomes a letter of the alphabet much
further down the line. Let's say that the user entered the text
Hello and if we tried a shift of five,
then the end result should be mjqqt
and the printed output from this function should say something like the encoded
text is this. And finally,
you're going to call the encrypt function and then pass in the user inputs from
over here
and you should be able to test the code and encrypt a message and see something
like this happen. Pause the video now, have a look through the starting code,
and once you're ready,
go ahead and tackle it and then come back and we'll go through the solution
together. Good luck. All right.
So I'm gonna fork a copy of the starting file and tackle to-do number one,
which is to create a function which is done using Def and it's called encrypt.
And this function is going to take two inputs. So inside the parentheses,
I'm going to put two parameters. Now you can,
of course use the same word as the inputs that you're going to put into it.
But in an ideal world,
you try to not confuse yourself between the arguments and the parameters.
So I'm going to call this plain_text,
this first parameter and the second parameter
I'm going to call it shift_amount. Now, of course, at a later date,
when we're sending the input to this function,
we're going to be sending the text to plain_text and shift to shift_amount.
Keeping the names of the argument and the parameter different will help us
later on to see which one is which. The next step is to go inside the encrypt
function, which of course means indenting by one,
we're going to shift each letter of the text forwards in the alphabet by the
shift amount, and then print the encrypted text.
This is going to be the challenging part of this whole exercise.
But essentially if we think about the problem,
what we want to do is we want to take each letter in the plain text.
So let's say all plain text is equal to hello.
Then we're going to take each of these letters in turn and then shift it up in
the alphabet by the shift amount. So let's say for example,
our shift amount was five and our plain text was hello,
the first thing we're going to do is we're going to take the H which is over
here, and then we're going to shift it up by five. So one, two, three, four,
five,
and we're going to turn it into an M. We can start off by writing a for loop that
loops through each of the letters in the plain text input.
And then we can take each of these letters and work out its position in the
alphabet list here.
And we can do that by using the index method.
So we can tap into a list, write .index,
and then put in the value that we want to get the index for
and we should be able to get that as the result. So back over here,
we're going to say,
alphabet, is the list, .index
and then inside the parentheses, we can put in our letter.
So we want to find out the index of the letter that we're looping through in
this alphabet up here. And once we've gotten a hold of that,
we're gonna save it into a variable called position.
Now, once we've got that, then we're going to calculate the new position.
Now the new position is just going to be the previous position,
which is going to be a number, and then plus the shift amount.
So while we're going through this code, let's look at this word
H. So the first time the loop runs letter's going to be equal to H, position is
going to be equal to zero one, two, three, four, five, six, seven.
And then the shift amount is five,
so seven plus five is going to be 12.
So now the new position is going to be 12,
and we can tap into the alphabet list at position 12,
so seven, eight, nine, ten, eleven, twelve,
and get hold of the value at that position.
So we can say that new_letter is equal to the alphabet at the new_position.
And now once we've got this new_letter,
we can create a empty string here which we'll call the ciphertext
which is going to be the encoded text. And it starts out being an empty string
and then once we've gotten each of the new letters,
we're going to add it to the cipher text. Now,
the final thing we have to do inside this encrypt function is once the
for loop has run its course,
and we've added all of the new letters into the cipher text,
then at this point, it's a good idea to print out the output.
And it tells us that we should say something like this,
where we replace this final thing with the actual encoded text,
which has of course, called a cipher text in our case.
Now let's just make that into an fstring
so that it'll insert it when it runs and we're ready to tackle the last to-do. So
here,
we're going to call the encrypt function and we're going to pass in all of the
user inputs.
So it remember that our parameter names are plain text and shift amount.
So I'm actually going to use the keyword arguments, so I'm going to say
plain text is equal to the text that the user has typed in.
And then on the next argument,
I'm going to say the shift amount is equal to the shift that the user has typed
in.
So now we're ready to test our code and let's take a look at what happens.
So I'm going to use this example that we've done here by trying to encode hello
with the shift of five. Now the first thing that we type, the direction,
doesn't actually matter because it's not being used anywhere in our code,
at least not yet. So you can type anything you want,
I'm just going to type encode. And then the message is going to be hello.
And the shift number is going to be five.
And now it tells us that the encoded text is mjqqt,
which matches with the expected output.
You might think that we're done, right? This is perfect. This is great
and we've completed this entire exercise.
But lets consider a different scenario.
What happens if we tried to encode a word that had letters that were really
close to the end of the alphabet?
Let's say that we wanted to encode the message zulu.
Now I'm only doing that because if there's a z in that word, of course.
And we wanted to shift it by five.
Now let's think of what's going to happen right? In our code,
we're going to take each of the letters, we're going to take z first
and then we're going to get its position in the alphabet and add the shift
amount to the position to get the new position.
Now, this is already at the end of the alphabet. So if this number
which is going to be 25,
because there's 26 letters in the alphabet and we start the position at zero.
So this plus five is going to be 30.
And what happens when we try to get the 30th item from the alphabet? Well,
you'll find out when I hit enter.
We get an index error and it tells us that list index out of range, and it
occurred on line 15.
So just as we predicted, this does not exist.
We have to try and tackle this right? So if you haven't thought about this,
this is the time to think about how can you solve this problem.
I want you to pause for a moment and try to see if you can fix it.
All right.
So we know that this has a fixed length and what we want to happen when we have
a letter towards the end that we need to encode is to loop back to the beginning
of the alphabet. So to solve this,
all we have to do is just to copy the existing letters in the alphabet and then
just duplicate it once more.
So this means that if we find a letter that's z and it needs to be shifted up
by five letters, it's going to continue until it gets to e.
Now,
the really wonderful thing about this index function is it's going to give you the
first index that it finds.
So if we're looking for the letter a, it's going to give you the index zero
and then it's going to stop.
It's not going to look for all the indices and find this one at position 26.
So it means the our code will now work
even if we have a word that have a lot of letters towards the end of the
alphabet. Let's go ahead and give it a spin,
Right?
Let's type encode. And our message is again,
zulu. And we hit enter.
And then we have a shift number of five and we get our encoded text.
z becomes one, two, three, four,
five, it becomes e, and u, one, two, three, four,
five, becomes z. So now we've solved that small bug.
Now there's a lot of other ways that you could have solved this bug,
but I think this is the easiest way without disturbing our logic here and not
making our function even longer.
If you had any problems with your code, then this is the time to go back and fix
it and make sure that you really understand everything that's going on here
before you move on to the next lesson. But once you're ready,
I'll see you on the next lesson where we're going to decrypt our code.