All right. So now that we can encrypt and decrypt our messages, well,
you might be asking, well, what else is there? We've completed the project,
right? Well, let's think about how we can improve our code.
Because if you take a look at the encrypt and decrypt functions,
you'll notice that a lot of the things that they're doing in it are very
similar, right? We've got some piece of empty string to begin with,
we loop through each of the letters in the text that we get passed over and we
get the position from the alphabet, get a new position,
and then add the new letter to that empty string.
Your challenge in this part of the Caesar cipher project is to combine the
encrypt and decrypt functions into a single function called Cesar.
And we're going to get rid of these if and elif statements,
and we're going to get rid of two functions that are overlapping.
And instead, we're just going to have a single function and we're going to call
it just once passing over text, shift, and direction.
So it's going to have three inputs and it's going to save us a lot of repetition
in our code.
Have a think about what you learned about functions with inputs and see if you
can complete this challenge. So pause the video now and give it a go.
Okay.
All right.
So, let's first create a new function and we're going to call it caesar.
This function is going to take three inputs this time.
Not only is it going to get the text, the shift,
but it's also going to get the direction.
So I'm going to call the first parameter start_text because we could be passed
the plain text or the cipher text. So let's just set it as the start text.
The next one I'm going to call the same
so I'm going to call it shift_amount. And then the last one
which is the direction, in order to differentiate it from this direction,
I'm going to make it a little bit longer.
So I'm going to call it the cipher_direction.
And now after a colon,
we're done with defining our function and we can start figuring out how we can
combine these two functions together. We know in both of them,
we need some sort of way of storing a piece of text
which starts out as empty
and then it ends up being the entire encrypted or decrypted text.
Let's create a new variable called end_text to contrast with a start_text.
And we started out as a empty string.
Now, the next thing we have to do is identical.
We have to loop through this text that we get passed over.
So let's create a for loop; for each letter in the start_text,
we're going to do something with each those letters. Namely,
we're going to get hold of the position of the letter in the alphabet.
So this line of code is actually identical across the two functions; position
equals alphabet using the index function to get hold of the index of the letter
that we're currently looping through.
The next part is a little bit tricky because on one hand when we're encrypting text,
we're shifting that position upwards by adding the shift amount.
But on the other hand, when we're decrypting,
we're subtracting the shift amount from the position.
So one way of doing this is, of course, by using an if statement. So you could
check if the cipher_direction was equal to encode. Well then in this case,
the new position is going to be equal to position plus the shift_amount.
And then you could do the else statement.
But there's actually an even easier way of doing this.
We can just check for if the cipher direction was decode.
And in this case, we take the shift amount and we multiply it by minus one.
So remember this line of code is exactly the same as
this, but it's just way more succinct.
And now outside the if statement,
we can define our new position and set it to equal the previous position plus
the shift amount.
And the reason why this works is because let's say that we had a shift amount of
five and we wanted to encode our text, well the new position is obviously going
to be previous position plus five. Now on the other hand,
if we were decoding, well this
if statement is going to trip and our five multiplied by minus one becomes minus
five. So now when we add -5 to
the previous position,
so let's say the previous position was 12 plus -5,
then a quick check with a calculator should get you a 7.
It's exactly the same as subtraction.
So now that we've gotten over that hurdle,
all we have to do is to tap into our end_text and to add to it by getting hold
of the letter at the new position
in the alphabet. And finally, once all of that's done,
we of course need to print the results.
Notice how previously we said the encoded text is and the decoded text is,
well, let's copy this string and paste it in here.
How can we change this part to be dynamic,
so depending on the cipher direction,
and then this part is easy because we can just replace it with the end_
text.
So one way is, remember that when the user is typing in the direction,
they're typing in either encode or decode. So we can delete everything other than
that 'd' and then dynamically insert the cipher direction that we get passed.
And now if we make that an fstring,
it should be able to insert encoded text is whatever the end text is, or
decoded
text is whatever it may be. Now we can go ahead and delete both of these
functions
and also this if statement and we can tackle to-do number two
and we can simply just call the Caesar function
passing over the start_text as the text the user inputted,
the shift_amount as the shift that they inputted,
and finally, the cypher direction as the direction that they inputted.
Now, we've cut down our code by a lot
and we've gotten rid of all the repeated bits of code and we've used what we've
learned about functions with inputs to vastly simplify our code.
We're getting the same function to do repeated actions,
but depending on the different inputs,
it does something completely different.
So now let's give our code a run and let's just make sure that everything works.
Encoding.
Okay.
And decoding.
So there's a bit of an issue there because when we get our decoded result back,
it says holvo instead of hello. So what's going on here?
Well, in my code that I wrote for the Caesar function,
I've accidentally introduced a bug. And as programmers,
none of us are free from this fault. We create bugs, but then we fix them.
So why don't you use this as a good debugging opportunity?
If you've been typing along with me,
I want you to take a look at the code or take a look at the screen and see if
you can figure out what is causing this aberrant decoded result.
Why are we getting holvo back instead of hello?
Where is the problem in our logic in our code?
See if you can solve this and fix the code so that we get back the decoded
results of hello. Pause the video and give that a go.
All right.
So this bug,
if we think about the logic, is caused because our
if statement where we check the cipher_direction and change the shift_amount
by multiplying it by minus one,
this line of code lives inside the for loop.
And we know about for loops that it's going to keep going again and again.
So for every letter in the start_text of which there are five,
every single time, this code is going to get executed and the cipher direction
is always decode so we keep multiplying that shift amount by minus one.
So the first one becomes decode,
the next one becomes encode, decode, encode, decode.
And that's why we're getting some parts of our hello back, but the 'o'
and the 'v' are the parts that are not right.
So in order to fix this, all we have to do is take our
if statement out of the for loop, put it before
the for loop right here.
And now if we run this again and we try to decode the same message,
so that was mjqqt, and we use the same shift number,
but now we get back the correct decoded results. Brilliant.
So did you manage to get that right? Don't worry
if you use a slightly different way of coding this logic up.
It really doesn't matter
as long as you managed to get it to work in the end and combine the two functions
into one. Once you're happy with step three,
head over to the next lesson and we'll tackle the final part
part four of the Caesar cipher
where we complete our project. For all of that, and more,
I'll see you on the next lesson.