All right, it's finally time to tackle our final project, the Caesar cipher.
As I've mentioned before, Caesar cipher is this way of encoding text that 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 becomes C, etc. 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.
We'll then E becomes h, f becomes I, g becomes J, etc. 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 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 going to choose a random one
and it gives us the encoded result.
So now if I take this encoded result 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.
All right, so once you're ready, let's get started building out the Caesar cipher.
Now you'll notice that I've split it into three parts.
So three, four, five are all the Caesar cipher.
And that's because there's quite a few things to do.
And I thought it might be a good idea to break it down into separate lessons so that we can go through
them individually and hopefully it'll make this very large problem.
A little bit easier.
And we practice that key engineering skill, which is breaking down a large problem into smaller problems,
and then taking a small problem, breaking it down into even smaller problems.
That is the only way that we can get through really, really hard problems.
All right.
So you'll notice that in Caesar cipher one I've split the tasks into four to dos.
You'll notice that order is a little bit weird because it goes four then three.
But they're roughly at the places where I expect you to write the relevant code.
So it's not a mistake.
It's actually very much on purpose.
Now, what I want you to do is to go through each of the to dos and read through their descriptions
on the right hand pane and try to solve them.
If you get stuck, there are some hints in there that will hopefully help you.
But essentially the really important thing is you actually read through the to dos, because some of
the things we might not have seen before.
For example, in order to complete the task, you will need to use a built in function called index.
And index is really handy because it allows us to find out the position of any item in a list.
So if we had a list of fruits and we know that they start counting from zero.
So apple is at position zero, pear is at one, orange is at two.
Then we can say fruits, the name of the list dot index.
And then in between the parentheses of this index function we pass in whatever it is we want to check.
So in this case pear would give us a position of one.
And we can verify this by printing it out.
So I can show this to you in the Python console, which is basically something that's completely separate
from the rest of our code.
And if I go ahead and now print this value, you can see it gives me one, which is what we would expect.
So this is going to be key for you to be able to solve this challenge, especially to do number two.
So look through the description.
It's there to help you.
And hopefully you'll be able to tackle each of these to do's.
And in order to figure out what it is that you're trying to aim for.
As always, you can right click on solution and run it without viewing the actual solution.
So we already have some pre-built in code which I've added in for you.
I wanted to save you the tedious task of typing out all the 26 letters of the alphabet, as well as
all the various inputs that we need.
So we need the user to tell us whether, if they want to encode or decode a message, and then we want
to actually ask them for the message, and then we want to ask them for the shift number.
So what you want to do is by the time you have entered these three pieces of information.
The next time I hit enter, it should give me a printed output.
Here is the encoded result and it gives me the encoded output.
So we're encoding.
Hello.
So h plus three letters I j k.
So h becomes k e plus three letters is f g h.
So this becomes h and so on and so forth.
Until we complete the entire text that we are supposed to encode by the shift number.
So that's all there is to it.
Look through the to dos, look through the description and try to complete part one of Caesar cipher.
I'll go quiet now and you can pause the video and tackle this challenge.
Cths.
All right.
Hopefully you have completed this challenge in its entirety.
If you haven't, I'm going to go through the solution together with you and hopefully by viewing the
solution, some things will become more obvious and you will be able to go back and tackle the code
by yourself.
So the first step is to create a function called encrypt.
I'm going to hide this right hand side pane just so that I can show you the to do in the entire line.
So we create a function with def, and then the function is going to be called encrypt.
And then we add a set of parentheses.
And in this case we want to have two inputs.
One is called original text and the other one is called shift amount.
And we add those if you remember, into the parentheses.
Finally we finish this function definition with a colon.
And then we can write something inside the function.
So to do number two is going to go inside the function.
So I'm just going to bring it up here to make it easier to see.
So inside the encrypt function we're going to shift each letter of the original text forwards in the
alphabet by the shift amount and print the encrypted text.
So there's a couple of things that we need to do.
So we're going to do them in several steps.
And the first step is to loop through each of the letters in this text.
So we can do that using a for loop.
So for letter in the original text we're going to go ahead and figure out what is the shifted position
for that letter.
So if we take the current alphabet and we use dot index.
And inside here we pass in the letter that we're currently looping through.
So let's start with a concrete example.
That way we can go through it line by line.
So if the original text was hello, and if the shift amount.
Was two, then the letter that we first loop through is h.
So h is going to be checked against this list called alphabet for its index.
So h is at position 01234567.
So h is at position seven right.
And this becomes seven.
And once we've gotten this number what we want to do is we want to shift it upwards.
Right.
And we want to shift it by the shift amount, which in our case we said was going to be two.
So h plus two is I j.
So it becomes j.
So how do we make it go to that position.
Well we take this index seven and we add the shift amount to it.
And then this means seven, then becomes nine, seven plus two becomes nine, and position nine is the
letter J, which is what we want.
So how can we turn this into the actual letter?
Well, firstly we need to probably save this shifted position into a variable so that we can use it
on the next line.
And what we're going to do is we're going to create a letter by using the alphabet list, using the
square brackets, and tapping to the item at this shifted position.
Now what do we do with this letter.
We actually need to accumulate it.
So in order to accumulate it we need to have some sort of empty string, which I will call ciphertext.
We started out as just an empty string with nothing in it.
And as always, it's very important that it's outside the for loop because we don't want it to reset
each time if it is inside the for loop.
So now going inside the for loop so indented inside here I'm going to set the ciphertext to plus equal
this new letter that we've found by this shifting process.
So at each of these steps I recommend printing out the values that you've got just to understand what
is going on behind the scenes.
So at this point we've now got the letter J from this.
And this J is being added to the ciphertext as the first value in there.
So it should look something like this.
Now the next step is to go ahead and for this loop to continue to the next cycle.
So we've now done H.
It's going to go to the next letter in the original text, which is E, and it's going to repeat this
process until we end up with all of the letters of the ciphertext.
And once it's done that, then we want to print it out.
So making sure that we are outside of the for loop.
So once the for loop has completed then we will go ahead and make a print statement.
And what we want to print is something like this.
Here is the encoded result.
Now if I go ahead and turn this into an F string then I can replace this with my ciphertext.
And now all I need to do is to actually call this function so that it gets triggered and it passes in
all of the user inputs from up here.
Now remember with functions, just defining it and running your code is not going to do anything.
It's just going to sit there until it is called upon.
So let's complete to do number three and complete this process.
So let's call this function encrypt and pass in the user inputs.
So I'm going to use a keyword arguments just to make it a little bit more clear.
So I'm going to say original text is equal to the text that was entered by the user.
And then the next value that I need to add in is the shift amount.
So here let's go ahead and add the shift amount.
And that's going to be set equal to whatever the user typed in here as an input.
So it's called shift.
And now I can run this file and go ahead and go through the process of encoding the word hello.
And I'm going to shift it by two again.
So now you can see the result is printed out.
And the first letter indeed does start with J.
And then it continues working out all of the other letters.
And you can see that there's two ends, because there are two L's in the original text.
So that all seems to be working just fine.
Now there is one slight problem which is highlighted by this to do number four.
What happens if you try to shift the letter Z forwards by nine?
Well let's try it.
Let's encode and we're going to encode just the letter z.
We're going to shift it by nine.
Well we get a classic error index error list index out of range.
So what's going on here.
Well if we take a look at our alphabet we know that Z has position 25.
And I know that because there's 26 letters in the alphabet.
If we start from zero, then Z is at 25.
So now we've got that position.
This is 25.
And then we add a shift amount.
We try to add nine to it.
So nine plus 25 is 34.
And then we're going to try and take 34 as the shifted position to go into the alphabet.
Now of course that is going to give us a index out of range error because there is nothing at position
34.
It ends at 25.
So how can we solve this problem?
Well, if you think about this problem, there's actually many, many ways that you can solve it.
Um, some are easier and some are more difficult.
But if you think back to when we learned about the modulo, you might remember that we can use the modulo
to get the remainder.
So let's go into the Python console again and let me show you.
So if we had the number four and I tried to modulo it or divide it by 25, it's going to give me four
because four does not divide by 25.
So the remainder is what we had at the beginning, which is four.
Now let's see what happens if we go over 25.
Let's say that we had 34 and we tried to modulo it by 25.
Well what's going to happen here is 34 divided by 25.
There is one set of 25 within 34.
And then it's going to give us the remainder.
So that means it's going to give us nine.
And this is really really useful because in our case this is exactly the behavior we want.
If we go outside the range just by modulating by the length of the alphabet, we can make sure that
no matter how far outside of the range of the list, we're always going to adjust it to within the length
from 0 to 25.
So to do this, all we need to do is take this shifted position and we're going to set it to equal the
previous version of shifted position modulo by the length.
So the number of items in our list called alphabet.
Now just as what you can do with plus equals where you take the ciphertext and add this alphabet to
it and then set it as the new value, you can do the same with modulo.
So we can actually change this to modulo equals to make our code even more short.
So shifted position modulo equals the length of the alphabet is going to make sure that we are always
within the range of 0 to 25.
And this is going to prevent that problem that we had earlier.
So if I go ahead and run the code and type Z and I shift it up by nine, you'll see that it's performing
the correct implementation, which is taking it all the way around and starting over here.
So Z plus nine is 123456789.
And it should as expected give us I.
Now this can be quite confusing.
This idea of the modulo um because it does involve a little bit of maths, but it's nothing that you
can't understand.
If you just try out some principles, try out some different letters, try out some different numbers,
experiment with it, and I'm sure that you'll eventually understand what's going on.
Don't move on until you actually understand what the code does.
Alternatively, you could have tried some of the other methods that I gave in the hint, and maybe one
of the other ones would be a little bit easier to understand for you, but I think this one should be
doable by most of the students.
As long as you invest a bit of time understanding how it works.
So that concludes part one of the Caesar cipher.
And we've got a functioning cipher that can now encode messages.
In the next part we're going to tackle how to decode messages and how to combine the encryption and
decryption functionalities together.
So for all of that and more I'll see you there.