All right, so now that we've managed to get our program to encode some text, it's time to do the opposite
direction, which is how do we get our program to be able to decode a piece of text based on a shift
number?
So let's say that we try to decode the letter G.
And we had a shift number of three.
Well in this case we actually have to work backwards and shift backwards in the alphabet in order to
get the letter.
That's three spaces previously.
So go ahead and open up the step two of our Caesar cipher project.
Now we've got all the code from the previous lesson, and we now have to complete three more to dos
to complete the decryption part of our program.
So the three to dos are you're going to create a function called decrypt that takes also two inputs.
Then inside the decrypt function, you're going to write some code to shift each letter of the original
text backwards in the alphabet, so that we decode it to the original text, and then we print out the
decrypted text.
Finally, we're going to figure out how to combine the encrypt and decrypt functions into a single function.
And then within the function itself, write some conditional statements to figure out whether if the
user wants to encrypt or decrypt using this direction variable, and then we end up with a single function
rather than two functions encrypt and decrypt.
So I recommend to do these to DOS in order and not to go straight towards to do number three, because
it's quite important that you understand how the decryption works and how it differs from encryption
before we move on to combining them.
So there are some hints in the description box as well, and there's some more details in the description
for you to read once you're ready.
Pause the video and I'll go silent and wait for you to complete this exercise.
All right, hopefully that went really well for you and you managed to figure it all out.
It's really, really hard.
So all of these projects are really hard because they don't just rely on what you've learned in class.
They also rely a lot of thinking, a lot of thinking outside the box, a lot of googling, a lot of
figuring things out, and also that key skill of engineering, which is breaking down problems to small
enough, a problem that you can solve.
So there is no way that you would be able to just breeze through any of these projects.
So don't feel bad if that's not happening.
But now I want to go through the solution with you in case you wanted to check your solution.
Or you need a couple of more hints to figure out what's going on.
So the first thing we're going to do is to create a function called decrypt.
And this function is again going to take two inputs the original text and also the shift amount.
So now that we're passing in these two variables inside the decrypt function we're going to shift each
of these letters by the amount that is required based on whatever it is the user wanted.
And then we're going to shift it backwards in the alphabet.
So if the original letter was F and their shift amount is two becomes H.
So now they're giving us h as the letter to decode.
Then we take the shift amount and we subtract two and to get back to the original letter.
So most of it is pretty much exactly the same as what happened in our encrypt.
But the only difference between decrypt and encrypt is the shift direction.
Because instead of taking the position of the current letter in the alphabet and shifting it up by adding
the shift amount, what we want to do is we want to subtract it by the shift amount so that instead
of going in this direction, we're now shifting it down this direction.
And the rest of the code is pretty much exactly the same.
Let's walk through them line by line, just so that we revise what's happening.
Firstly, we're looping through each letter in the text to decode.
We're working out the shifted position.
So the position of the new letter, by taking the current letter's position and subtracting it by the
shift amount.
Once we've got that shift to position, we're using the modulo to divide that position by the length
of the alphabet, which is 26.
26 letters.
And what that means is we're always going to be within that range of 0 to 25.
And we're never going to go out of range of our list, which is called alphabet.
Finally, we take our empty ciphertext and we add each of these letters as we loop through each of the
letters in the original text, and we end up with the final ciphertext.
Now, even though ciphertext is what worked for us previously in encrypt, it's probably not the best
wording in this case, because the ciphertext is the result that we get after we encrypt some plaintext.
So let's just change this to output text.
And we need to change it all the places that we use it, and now we're ready to go ahead and test our
decrypt function.
So let's go ahead and call decrypt and pass in the original text, which is going to be whatever the
user added to this input called text.
And then we also add the shift amount based on what the user typed for the shift.
So now let's go ahead and run our code.
And this part is still not considered.
So it doesn't actually matter what you type.
And then we're going to check just one letter.
So let's say that I have the letter A and I want to shift it down by two.
So remember even though we can type, encode or decode, the only function that's being called right
now in our code is decrypt.
So it should be taking my letter and shifting it down by two.
So if A is right here, then shifting it backwards will take it to Z and then to Y.
So now let's go ahead and hit enter.
And you can see that my decryption is working as expected.
So now all that we have left to do is to combine the encrypt and decrypt functions together into a single
one.
Because as you can see, they are extremely repetitive.
They're so repetitive that I was able to copy and paste the encrypt function into the decrypt function
to get the same functionality.
So let's go ahead and create this new function called Caesar.
And this function is going to take the original text as an input and also the shift amount as the input.
So same as before.
But in addition I'm also going to add the directionality into this function.
Now instead of calling it direction, which will be a little bit confusing because am I referring to
the direction variable here, or am I referring to the direction variable here?
And you can see as soon as I write that PyCharm is already giving me a warning, telling me that it
shadows the name direction from the outer scope, which means you're trying to create a new variable
name that's only going to be used inside your function, but you've already got this variable name declared
outside.
Now this will still work.
By the way, this is valid code, but it's just confusing code.
So let's change it.
Let's call this variable encode or decode.
It's pretty easy to understand right?
We're trying to figure out do we encode or do we decode in this function.
So I'm going to copy everything I had from the decrypt function and paste it inside my Caesar function.
And now what I want to do is to change it so that whether if the user wanted encode or decode, I will
make this function do the exact thing that they want and encode and decode the text.
So what is the difference?
Well, the only difference really is this line right here.
Either we subtract the shift amount when we're decoding or we add the shift amount when we're encoding.
So in maths, as you might have seen in the little hint I had here in maths, when you multiply a number
by minus one you will reverse its sign.
So positive five can become negative five by simply multiplying it by minus one.
And then if we add a negative number, that's the same as subtracting it.
So this might be a little bit of a mathematical knowledge that you may or may not know or may or may
not remember, as in my case, but hopefully with that hint, you could work out that in order to have
this line of code work, both during encoding and decoding, all we need to do is to change that shift
amount based on the encode or decode variable.
So if encode or decode is equal to decode well in this case we're going to take the shift amount and
we're going to multiply equals it by minus one.
So this is going to change this value to the opposite sign.
So if it was positive it becomes negative.
And that's what we want when we're decoding.
Now in this case instead of subtracting we can simply add.
So whatever it is when we add a negative number such as in the case of decoding, it's the same UVs
as subtraction, and when it is in code, then this if statement is not going to trigger and we have
the original code that we have for the encrypt function.
So this is a really neat way that you can get around this problem.
So now all that's left to do is to actually call this Caesar function instead of encrypt or decrypt.
So let's clear the code a little bit to make this file a little less confusing, so that we only have
our Caesar function.
And now I'm going to call this function Caesar and pass in the three variables that it expects original
text shift amount, encode or decode.
So text is the name that we had for the input of the message, and then the shift is the variable we
had for the shift amount input.
And finally we also have the direction.
So this is of course a positional argument way of calling this function.
But if you want it to be more clear then you can of course add in the keyword arguments rthy.
So now let's go ahead and run our code to see if it works.
So I'm first going to type encode to encode my message.
And I'm going to encode just a single letter to make our lives a little bit more easy.
So now I want to encode which means shifting it up.
And I want to shift it up by two.
So a then b, then C.
So hopefully I get the result c.
Now let's go ahead and rerun this, um, code.
So go ahead and run it again.
And this time I'm going to say decode.
So again I'm going to use the same letter A.
And I want to shift it by two.
And this time instead of getting C, I now have y because our code is working as expected, and we only
have a single function that achieves all of that functionality.
Now there's just one last thing that you might be bothered by if you have a little bit of OCD like me,
I don't like the fact that this prints out here is the encoded result, whether if it's decode or encode.
So how can we get it to say here is the encoded result when it is encoded.
And here is the decoded result when it is decode.
Well there's a really neat trick here because notice that the word encoded and decode it in English.
Actually, both have the word encode and decode, and the only thing that they have in common is the
D to represent the fact that it happened in the past.
So for us, it's as simple as simply taking this part, adding in the encode or decode variable, adding
D at the end no matter which one it is.
And this f string will now print out encoded result if it is encoded, and it will print out decoded
result when it is decoded.
So hopefully you manage to understand what's going on in the explanation or you already got it by yourself
anyways and you're just watching to check.
But once you're ready, we're going to move on to the next step and complete our Caesar cipher program.
So for all of that and more, I'll see you there.