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 backward and shift backward in the alphabet in order
to get the letter that's three spaces previously.
So in this case,
that would be D. Go ahead and head over to caesar-
cipher-2-start and fork your own copy of it.
Alright, so we've got all the code that we had at the end of part one,
but now we have to create the decrypt function.
And the decrypt function is again
going to take the text that the user inputted and the shift number,
and both of these are going to go into the function as inputs.
And then inside the function,
we're going to shift each letter of the text backward in the alphabet
by the shift amount. For example,
if we started out with this code and we needed to shift each letter by five,
then we end up with the plain text, hello. And finally,
our function needs to print
the decoded text is, depending on what it is that we actually got. Finally,
in to-do part three,
we're going to use that direction
variable that we ignored previously, where we ask the user to type encode
if they want to encrypt and decode if they want to decrypt.
And then we're going to use an if statement here to check which one they chose,
and then we're going to call the corresponding function based on that direction.
And once you've completed all three to-dos,
you should be able to run your program and encode a message
as well as decode a message when you hit run again,
like that.
Have a think about how you might tackle each of the three to-dos and then pause
the video and give it a go.
All right. So how did that go? The first part should be quite easy.
All we have to do is define a new function called decrypt
and this decrypt is going to take the same inputs as our encrypt,
which is the text and the shift that the user entered.
But again,
I'm going to use some different parameter names to be able to differentiate the
arguments and the parameter. So in this case,
because the text that I'm getting is going to be scrambled,
I'm going to call the first parameter cipher_text,
and the second parameter I'll call it again, the shift amount.
Now, after a colon, I'm done with to-do 1. Now let's move on to
to-do 2.
So here I have to be inside the decrypt function,
so indented inside of the function,
and then I'm going to shift each letter of the ciphertext backward in the
alphabet by the shift amount
and then print out the decrypted text. To do that I'm again
going to need a for loop,
and I'm going to loop through each letter in the cipher text.
And for each of those letters,
what I want to do is I want to get hold of the position that they are in the
alphabet. So I'm gonna check the alphabet and then use again,
that index function
and then tap into the position of the letter.
Once I've got the position, I'm going to define the new position
which is going to be the current position. Now,
remember, we're moving backwards in the alphabet now.
So I'm going to subtract the shift amount from the position.
Once that's done,
I can define a blank variable called plain text,
which is just going to be an empty string.
And once I've gotten hold of each of the new positions,
I can add to that plain text by tapping into the alphabet and then getting hold
of the letter at the new position. So now finally,
once that for loop's completed,
then I can print out the results and I'm going to use the same sort of output
structure as I've got in the example.
And we're going to replace that hardcoded text with the plain text that we've
decrypted. And finally,
I just have to turn this into an fstring so that it actually gets inserted.
Now, the last thing I have to do is the to-do number three.
So here we're actually gonna check the direction variable.
And we're going to see that if it's equal to encode, well
then we're going to call that encrypt method that we had previously,
so indented inside the if statement. But else
if the direction was actually equal to decode, well
in this case, we're going to call our newly created decrypt function.
And we're going to pass in the cipher text as the text and the shift amount is
going to be equal to the shift. So once again,
these arguments come from up here
when we ask the user to input those values.
And now it should go over to the decrypt function and do everything that's
inside to decrypt the text.
So now let's give our program a run and just make sure that it works as
expected. So I'm going to encode our message hello,
shift it by five, and then I'm going to run it again.
And I'm going to try and decode this message now.
So I'm going to type decode and I'm going to paste in that message we had before
and type the same shift number
and now we've got our text decoded.
So that's how you solve step 2 of our project.
Have a review of the code here and once you're ready,
head over to the next lesson and we're going to cover part 3 of the Caeser
cipher. So for all of that, and more, I'll see you on the next lesson.