1
00:00:00,780 --> 00:00:05,110
All right. So now that we've managed to get our program to encode some text,

2
00:00:05,470 --> 00:00:07,960
it's time to do the opposite direction,

3
00:00:07,990 --> 00:00:12,990
which is how do we get our program to be able to decode a piece of text based on

4
00:00:13,210 --> 00:00:14,043
a shift number.

5
00:00:14,470 --> 00:00:19,470
So let's say that we try to decode the letter G and we had a shift number of

6
00:00:19,690 --> 00:00:21,760
three. Well, in this case,

7
00:00:21,790 --> 00:00:26,790
we actually have to work backward and shift backward in the alphabet in order

8
00:00:27,700 --> 00:00:31,960
to get the letter that's three spaces previously.

9
00:00:32,320 --> 00:00:33,070
So in this case,

10
00:00:33,070 --> 00:00:37,510
that would be D. Go ahead and head over to caesar-

11
00:00:37,510 --> 00:00:41,860
cipher-2-start and fork your own copy of it.

12
00:00:43,000 --> 00:00:46,720
Alright, so we've got all the code that we had at the end of part one,

13
00:00:47,170 --> 00:00:50,170
but now we have to create the decrypt function.

14
00:00:50,890 --> 00:00:52,960
And the decrypt function is again

15
00:00:52,960 --> 00:00:57,960
going to take the text that the user inputted and the shift number,

16
00:00:58,810 --> 00:01:02,050
and both of these are going to go into the function as inputs.

17
00:01:02,710 --> 00:01:04,599
And then inside the function,

18
00:01:04,660 --> 00:01:09,660
we're going to shift each letter of the text backward in the alphabet

19
00:01:10,000 --> 00:01:12,100
by the shift amount. For example,

20
00:01:12,220 --> 00:01:17,220
if we started out with this code and we needed to shift each letter by five,

21
00:01:17,980 --> 00:01:22,450
then we end up with the plain text, hello. And finally,

22
00:01:22,510 --> 00:01:24,430
our function needs to print

23
00:01:24,700 --> 00:01:29,700
the decoded text is, depending on what it is that we actually got. Finally,

24
00:01:30,760 --> 00:01:32,440
in to-do part three,

25
00:01:32,770 --> 00:01:35,830
we're going to use that direction

26
00:01:35,830 --> 00:01:40,660
variable that we ignored previously, where we ask the user to type encode

27
00:01:40,720 --> 00:01:44,050
if they want to encrypt and decode if they want to decrypt.

28
00:01:44,650 --> 00:01:49,450
And then we're going to use an if statement here to check which one they chose,

29
00:01:49,930 --> 00:01:54,280
and then we're going to call the corresponding function based on that direction.

30
00:01:54,760 --> 00:01:57,580
And once you've completed all three to-dos,

31
00:01:57,670 --> 00:02:02,050
you should be able to run your program and encode a message

32
00:02:03,340 --> 00:02:06,970
as well as decode a message when you hit run again,

33
00:02:09,460 --> 00:02:10,293
like that.

34
00:02:10,990 --> 00:02:15,340
Have a think about how you might tackle each of the three to-dos and then pause

35
00:02:15,340 --> 00:02:16,630
the video and give it a go.

36
00:02:21,380 --> 00:02:25,850
All right. So how did that go? The first part should be quite easy.

37
00:02:26,150 --> 00:02:29,870
All we have to do is define a new function called decrypt

38
00:02:30,410 --> 00:02:35,270
and this decrypt is going to take the same inputs as our encrypt,

39
00:02:35,450 --> 00:02:39,710
which is the text and the shift that the user entered.

40
00:02:40,220 --> 00:02:41,030
But again,

41
00:02:41,030 --> 00:02:45,020
I'm going to use some different parameter names to be able to differentiate the

42
00:02:45,020 --> 00:02:47,540
arguments and the parameter. So in this case,

43
00:02:47,540 --> 00:02:51,080
because the text that I'm getting is going to be scrambled,

44
00:02:51,380 --> 00:02:53,990
I'm going to call the first parameter cipher_text,

45
00:02:54,620 --> 00:02:58,520
and the second parameter I'll call it again, the shift amount.

46
00:02:59,170 --> 00:03:03,640
Now, after a colon, I'm done with to-do 1. Now let's move on to

47
00:03:03,640 --> 00:03:04,630
to-do 2.

48
00:03:05,950 --> 00:03:09,520
So here I have to be inside the decrypt function,

49
00:03:09,580 --> 00:03:11,950
so indented inside of the function,

50
00:03:12,550 --> 00:03:17,230
and then I'm going to shift each letter of the ciphertext backward in the

51
00:03:17,230 --> 00:03:19,390
alphabet by the shift amount

52
00:03:19,630 --> 00:03:23,110
and then print out the decrypted text. To  do that I'm again

53
00:03:23,110 --> 00:03:24,310
going to need a for loop,

54
00:03:24,880 --> 00:03:28,990
and I'm going to loop through each letter in the cipher text.

55
00:03:29,710 --> 00:03:31,810
And for each of those letters,

56
00:03:31,870 --> 00:03:36,870
what I want to do is I want to get hold of the position that they are in the

57
00:03:37,390 --> 00:03:41,530
alphabet. So I'm gonna check the alphabet and then use again,

58
00:03:41,530 --> 00:03:43,210
that index function

59
00:03:43,630 --> 00:03:46,570
and then tap into the position of the letter.

60
00:03:47,950 --> 00:03:51,940
Once I've got the position, I'm going to define the new position

61
00:03:52,360 --> 00:03:55,570
which is going to be the current position. Now,

62
00:03:55,600 --> 00:03:58,420
remember, we're moving backwards in the alphabet now.

63
00:03:58,450 --> 00:04:02,680
So I'm going to subtract the shift amount from the position.

64
00:04:03,520 --> 00:04:04,720
Once that's done,

65
00:04:04,780 --> 00:04:09,220
I can define a blank variable called plain text,

66
00:04:09,640 --> 00:04:11,770
which is just going to be an empty string.

67
00:04:12,400 --> 00:04:15,790
And once I've gotten hold of each of the new positions,

68
00:04:16,149 --> 00:04:21,149
I can add to that plain text by tapping into the alphabet and then getting hold

69
00:04:22,780 --> 00:04:27,400
of the letter at the new position. So now finally,

70
00:04:27,400 --> 00:04:29,230
once that for loop's completed,

71
00:04:29,470 --> 00:04:34,470
then I can print out the results and I'm going to use the same sort of output

72
00:04:34,660 --> 00:04:37,150
structure as I've got in the example.

73
00:04:37,780 --> 00:04:42,780
And we're going to replace that hardcoded text with the plain text that we've

74
00:04:43,420 --> 00:04:45,280
decrypted. And finally,

75
00:04:45,280 --> 00:04:49,420
I just have to turn this into an fstring so that it actually gets inserted.

76
00:04:49,990 --> 00:04:54,130
Now, the last thing I have to do is the to-do number three.

77
00:04:54,640 --> 00:04:58,300
So here we're actually gonna check the direction variable.

78
00:04:58,930 --> 00:05:02,890
And we're going to see that if it's equal to encode, well

79
00:05:02,890 --> 00:05:07,180
then we're going to call that encrypt method that we had previously,

80
00:05:07,570 --> 00:05:11,410
so indented inside the if statement. But else

81
00:05:11,440 --> 00:05:16,180
if the direction was actually equal to decode, well

82
00:05:16,180 --> 00:05:20,740
in this case, we're going to call our newly created decrypt function.

83
00:05:21,280 --> 00:05:26,280
And we're going to pass in the cipher text as the text and the shift amount is

84
00:05:27,970 --> 00:05:31,360
going to be equal to the shift. So once again,

85
00:05:31,480 --> 00:05:34,390
these arguments come from up here

86
00:05:34,450 --> 00:05:37,180
when we ask the user to input those values.

87
00:05:37,780 --> 00:05:41,890
And now it should go over to the decrypt function and do everything that's

88
00:05:41,890 --> 00:05:43,630
inside to decrypt the text.

89
00:05:44,110 --> 00:05:47,980
So now let's give our program a run and just make sure that it works as

90
00:05:47,980 --> 00:05:52,240
expected. So I'm going to encode our message hello,

91
00:05:53,020 --> 00:05:56,530
shift it by five, and then I'm going to run it again.

92
00:05:56,930 --> 00:05:59,540
And I'm going to try and decode this message now.

93
00:06:00,950 --> 00:06:05,450
So I'm going to type decode and I'm going to paste in that message we had before

94
00:06:06,140 --> 00:06:08,600
and type the same shift number

95
00:06:09,020 --> 00:06:11,810
and now we've got our text decoded.

96
00:06:12,590 --> 00:06:16,310
So that's how you solve step 2 of our project.

97
00:06:16,970 --> 00:06:20,690
Have a review of the code here and once you're ready,

98
00:06:20,810 --> 00:06:25,040
head over to the next lesson and we're going to cover part 3 of the Caeser

99
00:06:25,040 --> 00:06:29,060
cipher. So for all of that, and more, I'll see you on the next lesson.

