1
00:00:00,360 --> 00:00:04,440
All right. So now that we can encrypt and decrypt our messages, well,

2
00:00:04,440 --> 00:00:08,010
you might be asking, well, what else is there? We've completed the project,

3
00:00:08,010 --> 00:00:11,670
right? Well, let's think about how we can improve our code.

4
00:00:12,300 --> 00:00:17,070
Because if you take a look at the encrypt and decrypt functions,

5
00:00:17,460 --> 00:00:21,510
you'll notice that a lot of the things that they're doing in it are very

6
00:00:21,510 --> 00:00:25,620
similar, right? We've got some piece of empty string to begin with,

7
00:00:25,890 --> 00:00:30,890
we loop through each of the letters in the text that we get passed over and we

8
00:00:31,470 --> 00:00:35,040
get the position from the alphabet, get a new position,

9
00:00:35,280 --> 00:00:38,550
and then add the new letter to that empty string.

10
00:00:39,390 --> 00:00:44,390
Your challenge in this part of the Caesar cipher project is to combine the

11
00:00:45,720 --> 00:00:50,720
encrypt and decrypt functions into a single function called Cesar.

12
00:00:52,140 --> 00:00:56,310
And we're going to get rid of these if and elif statements,

13
00:00:56,700 --> 00:01:01,170
and we're going to get rid of two functions that are overlapping.

14
00:01:01,230 --> 00:01:05,790
And instead, we're just going to have a single function and we're going to call

15
00:01:05,790 --> 00:01:10,350
it just once passing over text, shift, and direction.

16
00:01:10,800 --> 00:01:15,690
So it's going to have three inputs and it's going to save us a lot of repetition

17
00:01:15,720 --> 00:01:16,553
in our code.

18
00:01:17,130 --> 00:01:22,130
Have a think about what you learned about functions with inputs and see if you

19
00:01:22,170 --> 00:01:26,040
can complete this challenge. So pause the video now and give it a go.

20
00:01:29,720 --> 00:01:30,553
Okay.

21
00:01:31,130 --> 00:01:31,430
All right.

22
00:01:31,430 --> 00:01:36,350
So, let's first create a new function and we're going to call it caesar.

23
00:01:37,520 --> 00:01:40,970
This function is going to take three inputs this time.

24
00:01:41,300 --> 00:01:45,080
Not only is it going to get the text, the shift,

25
00:01:45,290 --> 00:01:47,180
but it's also going to get the direction.

26
00:01:47,930 --> 00:01:52,930
So I'm going to call the first parameter start_text because we could be passed

27
00:01:53,240 --> 00:01:58,070
the plain text or the cipher text. So let's just set it as the start text.

28
00:01:58,610 --> 00:02:00,260
The next one I'm going to call the same

29
00:02:00,260 --> 00:02:03,620
so I'm going to call it shift_amount. And then the last one

30
00:02:03,620 --> 00:02:08,389
which is the direction, in order to differentiate it from this direction,

31
00:02:08,720 --> 00:02:10,280
I'm going to make it a little bit longer.

32
00:02:10,430 --> 00:02:12,950
So I'm going to call it the cipher_direction.

33
00:02:14,360 --> 00:02:16,640
And now after a colon,

34
00:02:16,700 --> 00:02:21,700
we're done with defining our function and we can start figuring out how we can

35
00:02:21,800 --> 00:02:25,580
combine these two functions together. We know in both of them,

36
00:02:25,580 --> 00:02:29,270
we need some sort of way of storing a piece of text

37
00:02:29,570 --> 00:02:31,070
which starts out as empty

38
00:02:31,340 --> 00:02:35,720
and then it ends up being the entire encrypted or decrypted text.

39
00:02:36,260 --> 00:02:41,260
Let's create a new variable called end_text to contrast with a start_text.

40
00:02:42,680 --> 00:02:45,110
And we started out as a empty string.

41
00:02:45,890 --> 00:02:48,500
Now, the next thing we have to do is identical.

42
00:02:48,500 --> 00:02:51,950
We have to loop through this text that we get passed over.

43
00:02:52,310 --> 00:02:57,310
So let's create a for loop; for each letter in the start_text,

44
00:02:57,530 --> 00:03:00,640
we're going to do something with each those letters. Namely,

45
00:03:00,640 --> 00:03:04,090
we're going to get hold of the position of the letter in the alphabet.

46
00:03:05,230 --> 00:03:09,820
So this line of code is actually identical across the two functions; position

47
00:03:09,820 --> 00:03:14,820
equals alphabet using the index function to get hold of the index of the letter

48
00:03:16,870 --> 00:03:18,430
that we're currently looping through.

49
00:03:19,330 --> 00:03:23,920
The next part is a little bit tricky because on one hand when we're encrypting text,

50
00:03:24,250 --> 00:03:28,480
we're shifting that position upwards by adding the shift amount.

51
00:03:29,020 --> 00:03:31,420
But on the other hand, when we're decrypting,

52
00:03:31,690 --> 00:03:34,510
we're subtracting the shift amount from the position.

53
00:03:35,440 --> 00:03:39,700
So one way of doing this is, of course, by using an if statement. So you could

54
00:03:39,700 --> 00:03:44,700
check if the cipher_direction was equal to encode. Well then in this case,

55
00:03:47,440 --> 00:03:52,440
the new position is going to be equal to position plus the shift_amount.

56
00:03:54,220 --> 00:03:56,170
And then you could do the else statement.

57
00:03:56,710 --> 00:04:00,250
But there's actually an even easier way of doing this.

58
00:04:00,670 --> 00:04:05,620
We can just check for if the cipher direction was decode.

59
00:04:06,370 --> 00:04:11,370
And in this case, we take the shift amount and we multiply it by minus one.

60
00:04:14,530 --> 00:04:17,800
So remember this line of code is exactly the same as

61
00:04:18,089 --> 00:04:21,870
this, but it's just way more succinct.

62
00:04:22,560 --> 00:04:24,690
And now outside the if statement,

63
00:04:24,840 --> 00:04:29,840
we can define our new position and set it to equal the previous position plus

64
00:04:32,280 --> 00:04:33,810
the shift amount.

65
00:04:34,710 --> 00:04:39,710
And the reason why this works is because let's say that we had a shift amount of

66
00:04:40,530 --> 00:04:45,530
five and we wanted to encode our text, well the new position is obviously going

67
00:04:46,920 --> 00:04:50,460
to be previous position plus five. Now on the other hand,

68
00:04:50,460 --> 00:04:52,710
if we were decoding, well this

69
00:04:52,710 --> 00:04:57,710
if statement is going to trip and our five multiplied by minus one becomes minus

70
00:05:00,720 --> 00:05:05,370
five. So now when we add -5 to

71
00:05:05,370 --> 00:05:06,510
the previous position,

72
00:05:06,630 --> 00:05:10,920
so let's say the previous position was 12 plus -5,

73
00:05:11,250 --> 00:05:16,170
then a quick check with a calculator should get you a 7.

74
00:05:16,830 --> 00:05:19,860
It's exactly the same as subtraction.

75
00:05:20,850 --> 00:05:22,920
So now that we've gotten over that hurdle,

76
00:05:23,640 --> 00:05:28,640
all we have to do is to tap into our end_text and to add to it by getting hold

77
00:05:30,210 --> 00:05:33,300
of the letter at the new position

78
00:05:34,800 --> 00:05:38,940
in the alphabet. And finally, once all of that's done,

79
00:05:38,970 --> 00:05:41,700
we of course need to print the results.

80
00:05:42,480 --> 00:05:47,480
Notice how previously we said the encoded text is and the decoded text is,

81
00:05:48,780 --> 00:05:52,740
well, let's copy this string and paste it in here.

82
00:05:53,190 --> 00:05:57,800
How can we change this part to be dynamic,

83
00:05:57,950 --> 00:05:59,960
so depending on the cipher direction,

84
00:06:00,530 --> 00:06:04,790
and then this part is easy because we can just replace it with the end_

85
00:06:04,790 --> 00:06:05,623
text.

86
00:06:06,110 --> 00:06:10,370
So one way is, remember that when the user is typing in the direction,

87
00:06:10,670 --> 00:06:15,670
they're typing in either encode or decode. So we can delete everything other than

88
00:06:15,770 --> 00:06:20,770
that 'd' and then dynamically insert the cipher direction that we get passed.

89
00:06:25,130 --> 00:06:27,620
And now if we make that an fstring,

90
00:06:27,920 --> 00:06:32,920
it should be able to insert encoded text is whatever the end text is, or 

91
00:06:35,870 --> 00:06:36,590
decoded

92
00:06:36,590 --> 00:06:41,590
text is whatever it may be. Now we can go ahead and delete both of these

93
00:06:42,320 --> 00:06:43,153
functions

94
00:06:43,640 --> 00:06:48,640
and also this if statement and we can tackle to-do number two

95
00:06:50,570 --> 00:06:53,930
and we can simply just call the Caesar function

96
00:06:54,410 --> 00:06:59,180
passing over the start_text as the text the user inputted,

97
00:06:59,510 --> 00:07:03,440
the shift_amount as the shift that they inputted,

98
00:07:03,770 --> 00:07:08,770
and finally, the cypher direction as the direction that they inputted.

99
00:07:09,740 --> 00:07:12,350
Now, we've cut down our code by a lot

100
00:07:12,530 --> 00:07:17,180
and we've gotten rid of all the repeated bits of code and we've used what we've

101
00:07:17,180 --> 00:07:22,180
learned about functions with inputs to vastly simplify our code.

102
00:07:23,360 --> 00:07:26,780
We're getting the same function to do repeated actions,

103
00:07:27,080 --> 00:07:30,110
but depending on the different inputs,

104
00:07:30,650 --> 00:07:33,230
it does something completely different.

105
00:07:34,040 --> 00:07:38,390
So now let's give our code a run and let's just make sure that everything works.

106
00:07:38,930 --> 00:07:39,800
Encoding.

107
00:07:43,420 --> 00:07:44,253
Okay.

108
00:07:45,820 --> 00:07:47,110
And decoding.

109
00:07:48,910 --> 00:07:53,910
So there's a bit of an issue there because when we get our decoded result back,

110
00:07:54,280 --> 00:07:58,930
it says holvo instead of hello. So what's going on here?

111
00:07:59,860 --> 00:08:04,030
Well, in my code that I wrote for the Caesar function,

112
00:08:04,390 --> 00:08:09,130
I've accidentally introduced a bug. And as programmers,

113
00:08:09,190 --> 00:08:13,600
none of us are free from this fault. We create bugs, but then we fix them.

114
00:08:14,350 --> 00:08:17,110
So why don't you use this as a good debugging opportunity?

115
00:08:17,680 --> 00:08:19,210
If you've been typing along with me,

116
00:08:19,510 --> 00:08:24,340
I want you to take a look at the code or take a look at the screen and see if

117
00:08:24,340 --> 00:08:29,080
you can figure out what is causing this aberrant decoded result.

118
00:08:29,560 --> 00:08:32,409
Why are we getting holvo back instead of hello?

119
00:08:33,250 --> 00:08:37,900
Where is the problem in our logic in our code?

120
00:08:38,409 --> 00:08:43,090
See if you can solve this and fix the code so that we get back the decoded

121
00:08:43,090 --> 00:08:46,960
results of hello. Pause the video and give that a go.

122
00:08:50,560 --> 00:08:51,310
All right.

123
00:08:51,310 --> 00:08:52,570
So this bug,

124
00:08:52,900 --> 00:08:57,120
if we think about the logic, is caused because our

125
00:08:57,180 --> 00:09:02,180
if statement where we check the cipher_direction and change the shift_amount

126
00:09:02,430 --> 00:09:04,320
by multiplying it by minus one,

127
00:09:04,920 --> 00:09:08,130
this line of code lives inside the for loop.

128
00:09:08,790 --> 00:09:13,470
And we know about for loops that it's going to keep going again and again.

129
00:09:14,070 --> 00:09:18,420
So for every letter in the start_text of which there are five,

130
00:09:19,080 --> 00:09:23,940
every single time, this code is going to get executed and the cipher direction

131
00:09:23,940 --> 00:09:28,620
is always decode so we keep multiplying that shift amount by minus one.

132
00:09:29,250 --> 00:09:31,890
So the first one becomes decode,

133
00:09:31,950 --> 00:09:36,000
the next one becomes encode, decode, encode, decode.

134
00:09:36,270 --> 00:09:39,960
And that's why we're getting some parts of our hello back, but the 'o'

135
00:09:39,990 --> 00:09:43,440
and the 'v' are the parts that are not right.

136
00:09:44,190 --> 00:09:48,240
So in order to fix this, all we have to do is take our

137
00:09:48,270 --> 00:09:53,010
if statement out of the for loop, put it before

138
00:09:53,250 --> 00:09:54,780
the for loop right here.

139
00:09:55,380 --> 00:10:00,120
And now if we run this again and we try to decode the same message,

140
00:10:00,150 --> 00:10:04,230
so that was mjqqt, and we use the same shift number,

141
00:10:04,770 --> 00:10:09,150
but now we get back the correct decoded results. Brilliant.

142
00:10:09,300 --> 00:10:11,910
So did you manage to get that right? Don't worry

143
00:10:11,910 --> 00:10:15,750
if you use a slightly different way of coding this logic up.

144
00:10:16,110 --> 00:10:17,340
It really doesn't matter

145
00:10:17,340 --> 00:10:21,990
as long as you managed to get it to work in the end and combine the two functions

146
00:10:21,990 --> 00:10:25,890
into one. Once you're happy with step three,

147
00:10:26,220 --> 00:10:29,400
head over to the next lesson and we'll tackle the final part

148
00:10:29,400 --> 00:10:31,500
part four of the Caesar cipher

149
00:10:31,690 --> 00:10:35,460
where we complete our project. For all of that, and more,

150
00:10:35,550 --> 00:10:36,600
I'll see you on the next lesson.

