# Lesson video

In progress...

Hi, I'm Rebecca.

Your computing teacher for the programming part six unit.

For this lesson, you're going to need your Repler account, which you should have already set up with your parent or carers permission.

You're also going to need a pen and paper to make any notes and to answer the questions that I give you in the lesson.

It's also a really good idea to try and remove as many distractions as you possibly can.

So that you can really focus in this lesson.

Once you've got all of that ready, we can begin.

In this lesson, you will describe the dictionary data structure and you'll use a dictionary to produce a key-value pairs.

We're going to start off this lesson, with a little bit of code cracking.

So crack this secret message.

A secret message has been written below.

Try to crack the code, to see what the message is.

So, I'm not going to give you any hints or anything like that.

What I want you to do is to pau, pause the video, and just have a think what this message might be.

Let's take a look then.

So the answer was, Python is ACE don't you agree? So we're going to be looking at a Caesar's cypher, this lesson.

A cypher is a type of secret code where you swap the letters around, so that no one can read your message.

A Caesar cypher is one of the oldest and most famous cyphers.

It is named after Julius Caesar.

And just note, this makes it quite a poor choice, for encryption in modern times but it will make it a nice way to learn about cypher encryption.

A Caesar cypher uses a key.

In this example, this, uh, in this example, the key is plus two.

This means, that the inner wheel moved two letters to the right.

A key is used to lock and unlock the message.

The message cannot be read without the key.

So this means, that the inner wheel moves two letters to the right.

The inner wheel becomes the original message.

The outer wheel is used for the encrypted message.

And to encrypt the word CAT, you start by finding, C on the inner wheel and look at the outer wheel for the encrypted letter.

So C becomes E, and then it continues.

So you continue for each letter until you have finished encryption.

So C becomes E , A becomes C and T becomes V.

A dictionary data structure, can be used to create a Caesar cypher.

This is ideal because a dictionary maps one thing to another.

Unlike a record data structure, a dictionary data structure is dynamic.

You can change the size of the structure during execution.

This means that a dictionary, can be populated during execution.

The key value can hold the original character and the pair can hold the encrypted character.

So here's a question for you.

Take a look at this code.

What will be the output of this programme when it is executed? DEC, pause the video while I have a think about that.

So the output would be B.

You can add a new key value pairing to the dictionary using this piece of code.

So, what will be the output of this programme when it is executed? Pause the video while you have a think.

So if we look there, that's a piece of code that you can use, and then this would be the output.

So it's just printing the whole dictionary off.

So what I want you to do now, bearing those things in mind and using the code snippets as well, to help you.

What I want you to do, is a mini project.

Now this lesson, you'll be creating a Caesar cypher encryption programme.

You should, you should use the worksheet to help you with this.

So make sure you read everything carefully, have a good think about it.

Now this is a programming challenge.

So you are going to have to use, as well everything that you've learned so far in all parts of this programming unit, to try and help you do this as well.

What programming constructs are you going to need? Which code snippets that you're going to have to do, you're going to have to look at old programmes that you've done from other lessons to see and try and sort of feed into what, what, what you need to actually do.

So really slow down and think about it.

And don't worry about DV, trial and error printing variables to see what's in them, all those kinds of things.

Uh, you, you you've got this.

Okay? You can have a go and I'm going to go through the solution with you afterwards, but I want you to make sure that you really try your best now and have a go.

So pause the video where you have to get that.

Excellent.

Let's take a look at how you could have solved this problem.

Then I'm going to show you some codes.

So if you do want to look at this code afterwards, you can go to that short link there.

So it's ncc.

io forward slash Ks for hyphen Cesar cypher.

And that will show you the code that, I'm about to show you now.

Let's take a walk through this programme, then to see if we can figure out how it works, and you can use it as well to help you with your solution.

If you're still working on that, and you need a little bit of support, so you can access this too, from the short link, um, so that you can see properly and look through it.

Cause there's quite a lot to look at here.

So if we look at the very beginning, we can see there that we've got a function and that function is being defined here and it's got populate cypher and it's being passed an encryption key as well into it.

So, we know this wouldn't be executed straight away.

It were, this would be our first line in our main programme that is executed here.

So we've got here print, what is in encryption keys? That's to be first thing that's asked, then we've got encrypt keys, we've got a variable.

And it's, it's, uh, going to assign that as an integer input.

And then we've got here, a variable called Caesar, and this is where that function call is happening.

So what it's going to do is, It's probably going to populate the cypher, uh, with the encryption key.

That's been entered here, so that's going to be passed.

So now we are going to go all the way up here, to this function.

So if I typed in a two here, then the, the value, two would be passed to this function unused.

And then if you look, you've got a bit of commenting here.

So that first part is just Caesar equals.

Um, and then just a blank dictionary because it's saying that, well, it's not blank cause it's got a space in it.

So a space will always be the same.

So, if it's a space that is going to record it as a space, then you've got this four loop here.

So this four loop, is all these lines of code here, this whole block.

And you've got a four X in range, 65 to 91, and this all links to the ASCII table.

So if you think about the ASCII table, you've got values on there, all the way from all the letters of the alphabet, plus all the other characters as well, that are used in the English dictionary.

But we're just looking at those capital letters, at the moment.

So 65 to 90 bond, uh, is all those letters of the alphabet.

So that's what the range is used there.

Say up to 90, wouldn't it because 91 is the stopping value.

You've then got here, key equals char and then X is placed there within the parameters.

So we're using this range.

Who's going to first time, it will be 65.

Um, so what is going to do, is convert it.

So what we can actually do, if we're not sure what that's doing, we can run this programme and just print key, and then you can see, so probably should have run it the first time.

You can see what it is.

So if I just put a three there, what it's doing now is it's printing the key.

Which is this one here.

So, it's starting off with the star of the alphabet, which is A and it's going all the way through the alphabet all the way to Z.

And that's what's happening there.

So in our four loop, just that tiny bit of code, there is going through the alphabet, from A, all the way to Z.

So ignore rest of the code while you're looking at that.

Just that one bit, that stop it at the four loop.

Now I know what that's doing.

I can just get rid of this line of code because I don't need to take any more and I can start looking at the next bit.

So the next bit, is an if statement or an if else statement here and we've got, if X plus incorrect key is greater than 90.

I wanted to restart the wheel and you can, you can put there restarting the wheel, is in the comments there.

So that's going to give you an idea of what's happening, in that little bit of code there.

But if you think about the Caesar cypher, if you have, um, Z, for example, and your key is two, you don't want it to run out of letters, or start going to the other parts of the ASCII table, where I'm just going to have a look.

What said I've got it up on my screen.

So the next one would be a square brackets.

You don't want to have a square bracket.

Well, you might do a, if you want to make it a bit more complicated, but we're doing the Caesar cypher.

So we need to go back to eight.

And if it was plus two Fianna Zed, and it was plus two, then it would go all the way round and it would be a B.

So we need to make sure that it restarts the wheel.

So this is checking.

So if X, so if you think about, if we were looking at that, say if we'd got to 90 all the way through the situation, so 90, plus the encrypt key.

So if I start, typed in a two, so then it would be 92.

So if 92 is greater than 90, which it is, then what it's going to do, is it's going to restart the wheel.

So it's going to minus 26, to take it back to the beginning.

Otherwise, if it's, if it's not greater than 90, then it's going to just encrypt it as it is.

Uh, cause you don't need to restart the wheel at that point.

Then you've got this bit of code, which is assigning pair to the key.

So if we look there, what is pair? What is the encrypted value? So then it's going to return the Caesar.

So it's going to do this four loop It's going to go through a free letter of the alphabet.

And then it's going to assign a key to each letter.

So using that dictionary and then it's going to return it.

Now, if I want to actually see what it looks like, I can actually print Caesar, here.

So I can actually see what's happened.

Have I spelled that right? I'm so rubbish at spelling Caesar because it's such an odd one to spell, A-E-S-A-R.

Okay.

So, now that I've printed it, if I just run it again, if I just type in my encryption key of two, you can see, this is exactly what's returned by Caesar.

So we've got that dictionary.

So if it's a zero, if it's a space, not a zero.

If it's a space, then it's going to be a space.

But if it's an eight, it's going to be C.

If it's a B, it's going to be D.

If it's a C it's going to be A and all the way through those letters, the output alphabet.

And you can see when it's got to, uh, uh, X it's gone Z, Y is A and Z is B , so you can see it's working and you can see, that it is restarted that wheel as well.

So that is what that whole populate cypher function, is actually doing.

And it's returning that Caesar's cypher.

There we go.

So all of that just, that was those three lines of code.

And then it went to the function.

It did all of that and he returned it and to hold it in the variable Caesar, then it says, enter your text to encrypt.

That's quite an easy one to understand isn't it.

And then plain text, and you can type it in.

And it's forcing that plain text to be an upper, because this is only using those Upper Case letters.

It's not using Lower Case as well.

Uh, you could, if you wanted to modify it later to do it for both, if you wanted to, just change the range that you've got there, but this is just looking at capital letters and it starts off with the cypher text, being just initialised as blank, because then it's going to be added to later on.

So every letter in the plain text is going to be encrypted, using those data pairings in the diction rate.

And then it just prints out the cypher text at the end.

So I can actually type in a message now.

So if I want the encryption to be two and I want it to type, uh, Python is A, just like we had at the beginning of the lesson, there is the code that you would use.

Let's just finish off then with a quick prediction.

Let's just see how, how much she can remember from something that was in quite a while ago.

Um, if a user enter 65 during the execution of this programme, what will be the output? So have a good look, pause the video and have a think about what it might be.

Let's take a look at the answer then.

So the answer was A, and if you remembered how chart actually works, it's all to do with the ASCII table and the decimal number in the ASCII table for the letter A is 65.

So that is why the output would be 65.

Well done.

That was quite a challenging project, this lesson.

So hopefully you got through it and maybe you needed a bit of support, uh, and that's okay too.

If you still need a little bit of support at this point, that's absolutely fine.

You learning new things all the time.

So you still going to need that little bit of help, here and there.

And if you'd like to, please ask your parent or carer, to share your work on Instagram, Facebook, or Twitter, tagging @Oaknational and #learnwithOak, because it's great to see what you've been getting into.

And I'll see you again soon for lesson three.