All right, we're about to cross the finish line. We're onto the last part of our Caesar cipher
and we're going to complete it in this lesson. First
go ahead and fork your own copy of part four of the Caesar cipher starting code.
And notice, firstly, we've got an extra file here called art.
And this, if you expand it fully, you should see an ASCII art of our logo.
Now, what we want to be able to do in this lesson is just to tidy up some of the loose ends, improve
the user experience and fix some of the bugs that you may or may not be aware of. To-do
#1 tells us to import and print the logo from art.py and this is going to show up when we first
load up our program like this.
Next, we're going to tackle this one. One of the things we haven't really thought about is if the
user enters a shift number that's actually really, really large like 100, then we're actually going
to run out of letters in our alphabet.
And so we're going to use what you've learnt before
and it might take a little bit of thinking around this to fix this particular bug right here.
And once you've done that, then we move on to over here and we think about what to do if the user wanted
to type a number, a symbol or a space.
So we want to preserve the symbols, space, or number that they typed in
and we're only going to use our cipher on the rest of the message,
so only the alphabetical letters. And everything else we're going to leave the same.
So, for example, if we had the text meet me at 3, then the end_text will keep the spaces where
they were, keep the numbers where they were.
And also if there were any symbols, we would also keep that in our final end_text.
And it's only the letters which will get scrambled.
And then finally, to-do #4, we're going to figure out how to ask the user if they want to
restart the cipher program.
So notice how in the final version, when we run this once, we can actually type yes
to go again.
This way we can keep encoding and decoding as much as we want until we're actually done when we can
type no
and it says goodbye
and that's the end of the code.
What we want to be able to do is instead of having to run our code again and again, we're going to
figure out a way of getting our code to be rerun
if the user typed yes to this question. And if they typed no, then and only then do we actually end
the program.
It might take a little bit of thinking to figure out how to solve all of these to-dos.
But once you're ready, if you're up for the challenge, then pause the video now and give it a go.
All right, so let's tackle them one by one. The first one you've already done before.
All it needs is to import the art file.
And if we want to be able to use the logo variable inside the art file, this one, then we can also
simply just write from art import logo.
So now we can print this logo right here.
So that's to-do #1 completed, now let's do the next one. This one requires us to figure out some
sort of clever solution so that when a user enters a shift that's greater than the number of letters
in the alphabet
over here, we have some sort of way of getting it back within the range of 0 to 25.
And I want you to see what happens when I type a message and I put a shift that's really large. You
can see we get an index out of range error because there is no letter at position 77 from the alphabet.
So that's why this line 14 over here actually breaks down.
If you have trouble with this, I gave you a hint where I wanted you to think about how you can use
the modulus.
So if we take 45 and we use the modulus operator to divide 45 by 26, which is the total number of letters
in the alphabet, then we should end up with the remainder.
Now, if you imagine we're dividing 45 by 26, then 26 goes into 45 once,
so we will get one, but we will have a remainder of 19.
So this is what we get from the modulus.
We would get the result to be 19, which is just the remainder.
And effectively, what this code does is it's going to divide a number by 26 as many times as needed
until we get to the final point where it can no longer be divided fully and we end up with a remainder.
And that remainder allows us to shift the shift number that's entered into something that fits in with
the number of letters of the alphabet that we have in here.
So then our code that we would have here is to modify the shift by taking the shift that the user entered
up here and then using the modulus to divide it by 26 as many times as needed until we get the remainder.
So now if we test this out, you can see that no matter how long my shift number is, let's try 87, we're
always going to get a result back because it's shifting that number down to fit within our alphabet.
And once you're done, let's move on to to-do #3.
So here we want to be able to allow any number, symbol or space, basically anything that's not within
the alphabet to go through and end up in the end_text.
But if it is one of the letters inside this list alphabet, then and only then do we actually want to
do the shifting and encoding.
In order to achieve this, we should probably use an if statement to say if the char...
So notice how I've changed the for loop to instead of writing for letter in start_text which kind
of assumes that everything in the start_text is a letter, to char which stands for character instead
because a character could be a number, could be a symbol, could be a letter, could be space.
And then once we get hold of the char or the character, we're going to check to see if the char is
in the alphabet.
So if it is indeed one of these letters and not some other random character, well then and only then
do we actually want this code to be executed.
So let me just make sure all the indentation is correct. And if it's not inside the alphabet, so else,
well then, in this case, we actually want to make the end_text append the actual character,
so without any modification.
Now, if we test this code by running it, so let's go ahead and encode. And I'm going to encode something
like Hello123%^ 321.
And now when I hit enter and I give it a shift, then you can see that the encoded result has all the
letters, all the things are in that list, the alphabet encoded.
But everything else, the numbers and the symbols are all left as they are, because more often than
not, your user might want to write a message like meet me at five or something, something exclamation
mark.
And this is just a very simple way of making it work.
Now, of course, there's a lot of other ways that you could have solved this.
So you could have, in fact, added a whole bunch of symbols and numbers and space and other characters
to this list
And instead of calling it alphabet, it could have been the characters list and then you could have
encoded all of that.
But that's actually not what the toO-do asks for. It wants us to let through the numbers, symbols, space
and only encode the letters.
So now all we have left is the last to-do, and this is probably going to be the most difficult one out
of the lot. What we want to do here is we want to be able to ask the user whether if they want to go
again, so whether if they want to encode or decode another message. And what happens in the final version,
the completed version of the app, is if we encode a message
and we get the result back, it doesn't just end, it doesn't exit because otherwise you would see that
message down here run again.
Instead it continues and it asks us, well, do you want to go again? So we could write
yes.
And it lets us again encode or decode a message.
We can basically go on and on and on and repeating this program until we want to exit.
And it's at that point when we can no longer type anything into our program and our code ends and we'd
have to run the whole thing again in order to use it again.
So at the moment, the part of our code that actually needs to be repeated if the user wanted to restart
it is actually just this part.
So the first thing I'm going to do is I'm actually going to shrink it down a bit.
And now all we're left with is 1, 2, 3, 4, 5 lines of code which needs repeat if
we're going to get the user to reuse the Caesar cipher.
What if we go ahead and indent this and put it inside a while loop?
What are we going to check to make sure that our loop continues?
Well, we could create a flag called should_continue and we start this out to be true.
And then while this is still true, we continue to keep looping through this so that the user gets to
input a new direction, new text, new shift and call the Caesar cipher on that again.
When should we stop it though?
Well, if at the end of deciphering or encoding their message, we ask them a question, something like
type
yes
if you want to go again or type no
if you don't want to, we could put that inside an input.
Like so. And we can save their answer inside a variable. Let's just call that result. So now we can
use an if statement to check
well, if the results is equal to no,
so when they actually want to stop and they want to exit the while loop, well, at this point we can
switch
that should_continue to false.
And once that's done, then this means that this while loop, when it checks it, this is going to be
false and it will no longer continue.
Just as a final touch, we can even tell the user goodbye
so that they know that the program has ended.
Now let's go ahead and run our code and see if it works.
All right, so let's first encode something. And now I can type yes to go again, and you can see it's
allowing me to type encode or decode.
So let's encode something else.
Now, finally, if I decided I've had enough, I'm done with encoding and decoding, I'm going to type
no.
It tells me goodbye and I can now see my prompt, which means it's actually exited out of the program
and I would need to run it again in order to restart it.
Did you manage to get that right?
If you want to take a look at the completed code, then you can head over to caesar-cipher-4-end and
you will find the same code as I've got here.
And of course, feel free to play around with the code, modify it and make this program your own.
So if you wanted to add some extra features, if you wanted to make sure that the numbers and symbols
are all being encrypted and any sort of bells and whistles and anything else that you can think of,
this is the time to do it now.
But once you're done, be sure to get some rest, because tomorrow is yet another packed session where
we're going to talk more about Python dictionaries.
For all of that and more, I bid you good night and I'll see you tomorrow.