1
00:00:00,300 --> 00:00:06,689
All right, we're about to cross the finish line. We're onto the last part of our Caesar cipher

2
00:00:06,720 --> 00:00:10,630
and we're going to complete it in this lesson. First

3
00:00:10,950 --> 00:00:16,440
go ahead and fork your own copy of part four of the Caesar cipher starting code.

4
00:00:16,840 --> 00:00:20,670
And notice, firstly, we've got an extra file here called art.

5
00:00:21,090 --> 00:00:26,370
And this, if you expand it fully, you should see an ASCII art of our logo.

6
00:00:27,880 --> 00:00:33,880
Now, what we want to be able to do in this lesson is just to tidy up some of the loose ends, improve

7
00:00:33,880 --> 00:00:40,260
the user experience and fix some of the bugs that you may or may not be aware of. To-do

8
00:00:40,270 --> 00:00:47,710
#1 tells us to import and print the logo from art.py and this is going to show up when we first

9
00:00:47,710 --> 00:00:49,690
load up our program like this.

10
00:00:51,210 --> 00:00:57,720
Next, we're going to tackle this one. One of the things we haven't really thought about is if the

11
00:00:57,720 --> 00:01:04,830
user enters a shift number that's actually really, really large like 100, then we're actually going

12
00:01:04,830 --> 00:01:06,750
to run out of letters in our alphabet.

13
00:01:07,140 --> 00:01:10,320
And so we're going to use what you've learnt before

14
00:01:10,500 --> 00:01:16,810
and it might take a little bit of thinking around this to fix this particular bug right here.

15
00:01:17,670 --> 00:01:24,870
And once you've done that, then we move on to over here and we think about what to do if the user wanted

16
00:01:24,870 --> 00:01:28,340
to type a number, a symbol or a space.

17
00:01:28,680 --> 00:01:32,880
So we want to preserve the symbols, space, or number that they typed in

18
00:01:33,240 --> 00:01:39,440
and we're only going to use our cipher on the rest of the message,

19
00:01:39,480 --> 00:01:44,040
so only the alphabetical letters. And everything else we're going to leave the same.

20
00:01:44,160 --> 00:01:51,450
So, for example, if we had the text meet me at 3, then the end_text will keep the spaces where

21
00:01:51,450 --> 00:01:54,480
they were, keep the numbers where they were.

22
00:01:54,630 --> 00:01:59,670
And also if there were any symbols, we would also keep that in our final end_text.

23
00:02:00,030 --> 00:02:02,840
And it's only the letters which will get scrambled.

24
00:02:03,990 --> 00:02:10,680
And then finally, to-do #4, we're going to figure out how to ask the user if they want to

25
00:02:10,680 --> 00:02:12,360
restart the cipher program.

26
00:02:12,900 --> 00:02:19,530
So notice how in the final version, when we run this once, we can actually type yes

27
00:02:19,740 --> 00:02:20,550
to go again.

28
00:02:21,030 --> 00:02:27,270
This way we can keep encoding and decoding as much as we want until we're actually done when we can

29
00:02:27,270 --> 00:02:28,050
type no

30
00:02:28,290 --> 00:02:29,400
and it says goodbye

31
00:02:29,430 --> 00:02:31,320
and that's the end of the code.

32
00:02:32,040 --> 00:02:37,740
What we want to be able to do is instead of having to run our code again and again, we're going to

33
00:02:37,740 --> 00:02:42,690
figure out a way of getting our code to be rerun

34
00:02:43,140 --> 00:02:51,990
if the user typed yes to this question. And if they typed no, then and only then do we actually end

35
00:02:51,990 --> 00:02:52,740
the program.

36
00:02:53,610 --> 00:02:58,440
It might take a little bit of thinking to figure out how to solve all of these to-dos.

37
00:02:58,890 --> 00:03:04,380
But once you're ready, if you're up for the challenge, then pause the video now and give it a go.

38
00:03:07,050 --> 00:03:12,390
All right, so let's tackle them one by one. The first one you've already done before.

39
00:03:12,480 --> 00:03:15,930
All it needs is to import the art file.

40
00:03:16,410 --> 00:03:25,380
And if we want to be able to use the logo variable inside the art file, this one, then we can also

41
00:03:25,380 --> 00:03:30,900
simply just write from art import logo.

42
00:03:32,310 --> 00:03:35,720
So now we can print this logo right here.

43
00:03:37,880 --> 00:03:46,040
So that's to-do #1 completed, now let's do the next one. This one requires us to figure out some

44
00:03:46,040 --> 00:03:53,150
sort of clever solution so that when a user enters a shift that's greater than the number of letters

45
00:03:53,480 --> 00:03:54,530
in the alphabet

46
00:03:55,500 --> 00:04:01,620
over here, we have some sort of way of getting it back within the range of 0 to 25.

47
00:04:02,590 --> 00:04:09,940
And I want you to see what happens when I type a message and I put a shift that's really large. You

48
00:04:09,940 --> 00:04:17,519
can see we get an index out of range error because there is no letter at position 77 from the alphabet.

49
00:04:17,920 --> 00:04:23,110
So that's why this line 14 over here actually breaks down.

50
00:04:24,730 --> 00:04:31,000
If you have trouble with this, I gave you a hint where I wanted you to think about how you can use

51
00:04:31,000 --> 00:04:31,840
the modulus.

52
00:04:32,410 --> 00:04:42,130
So if we take 45 and we use the modulus operator to divide 45 by 26, which is the total number of letters

53
00:04:42,130 --> 00:04:46,000
in the alphabet, then we should end up with the remainder.

54
00:04:46,690 --> 00:04:53,110
Now, if you imagine we're dividing 45 by 26, then 26 goes into 45 once,

55
00:04:53,350 --> 00:04:57,910
so we will get one, but we will have a remainder of 19.

56
00:04:58,510 --> 00:05:00,990
So this is what we get from the modulus.

57
00:05:01,030 --> 00:05:05,080
We would get the result to be 19, which is just the remainder.

58
00:05:05,680 --> 00:05:13,030
And effectively, what this code does is it's going to divide a number by 26 as many times as needed

59
00:05:13,330 --> 00:05:19,690
until we get to the final point where it can no longer be divided fully and we end up with a remainder.

60
00:05:20,020 --> 00:05:27,470
And that remainder allows us to shift the shift number that's entered into something that fits in with

61
00:05:27,470 --> 00:05:30,850
the number of letters of the alphabet that we have in here.

62
00:05:31,060 --> 00:05:37,630
So then our code that we would have here is to modify the shift by taking the shift that the user entered

63
00:05:37,630 --> 00:05:45,700
up here and then using the modulus to divide it by 26 as many times as needed until we get the remainder.

64
00:05:45,940 --> 00:05:53,050
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

65
00:05:53,050 --> 00:05:59,880
always going to get a result back because it's shifting that number down to fit within our alphabet.

66
00:06:00,280 --> 00:06:03,110
And once you're done, let's move on to to-do #3.

67
00:06:03,820 --> 00:06:11,800
So here we want to be able to allow any number, symbol or space, basically anything that's not within

68
00:06:11,800 --> 00:06:16,410
the alphabet to go through and end up in the end_text.

69
00:06:16,900 --> 00:06:23,500
But if it is one of the letters inside this list alphabet, then and only then do we actually want to

70
00:06:23,500 --> 00:06:25,750
do the shifting and encoding.

71
00:06:26,620 --> 00:06:33,210
In order to achieve this, we should probably use an if statement to say if the char...

72
00:06:33,970 --> 00:06:40,060
So notice how I've changed the for loop to instead of writing for letter in start_text which kind

73
00:06:40,060 --> 00:06:46,270
of assumes that everything in the start_text is a letter, to char which stands for character instead

74
00:06:46,600 --> 00:06:51,460
because a character could be a number, could be a symbol, could be a letter, could be space.

75
00:06:52,060 --> 00:07:00,100
And then once we get hold of the char or the character, we're going to check to see if the char is

76
00:07:00,100 --> 00:07:01,960
in the alphabet.

77
00:07:03,220 --> 00:07:11,200
So if it is indeed one of these letters and not some other random character, well then and only then

78
00:07:11,200 --> 00:07:15,430
do we actually want this code to be executed.

79
00:07:16,860 --> 00:07:24,840
So let me just make sure all the indentation is correct. And if it's not inside the alphabet, so else,

80
00:07:25,260 --> 00:07:33,120
well then, in this case, we actually want to make the end_text append the actual character,

81
00:07:33,150 --> 00:07:35,370
so without any modification.

82
00:07:36,570 --> 00:07:43,860
Now, if we test this code by running it, so let's go ahead and encode. And I'm going to encode something

83
00:07:43,860 --> 00:07:51,120
like Hello123%^ 321.

84
00:07:51,450 --> 00:08:01,260
And now when I hit enter and I give it a shift, then you can see that the encoded result has all the

85
00:08:01,260 --> 00:08:07,020
letters, all the things are in that list, the alphabet encoded.

86
00:08:07,530 --> 00:08:14,010
But everything else, the numbers and the symbols are all left as they are, because more often than

87
00:08:14,010 --> 00:08:19,440
not, your user might want to write a message like meet me at five or something, something exclamation

88
00:08:19,440 --> 00:08:19,850
mark.

89
00:08:20,130 --> 00:08:23,910
And this is just a very simple way of making it work.

90
00:08:24,600 --> 00:08:27,870
Now, of course, there's a lot of other ways that you could have solved this.

91
00:08:28,320 --> 00:08:34,710
So you could have, in fact, added a whole bunch of symbols and numbers and space and other characters

92
00:08:35,070 --> 00:08:36,510
to this list

93
00:08:36,659 --> 00:08:43,440
And instead of calling it alphabet, it could have been the characters list and then you could have

94
00:08:43,440 --> 00:08:44,910
encoded all of that.

95
00:08:45,360 --> 00:08:51,600
But that's actually not what the toO-do asks for. It wants us to let through the numbers, symbols, space

96
00:08:51,900 --> 00:08:54,540
and only encode the letters.

97
00:08:56,350 --> 00:09:02,770
So now all we have left is the last to-do, and this is probably going to be the most difficult one out

98
00:09:02,770 --> 00:09:11,800
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

99
00:09:11,800 --> 00:09:20,320
again, so whether if they want to encode or decode another message. And what happens in the final version,

100
00:09:20,320 --> 00:09:25,870
the completed version of the app, is if we encode a message

101
00:09:27,000 --> 00:09:34,020
and we get the result back, it doesn't just end, it doesn't exit because otherwise you would see that

102
00:09:34,020 --> 00:09:35,870
message down here run again.

103
00:09:36,330 --> 00:09:41,810
Instead it continues and it asks us, well, do you want to go again? So we could write

104
00:09:41,820 --> 00:09:42,320
yes.

105
00:09:42,750 --> 00:09:46,050
And it lets us again encode or decode a message.

106
00:09:47,190 --> 00:09:53,910
We can basically go on and on and on and repeating this program until we want to exit.

107
00:09:54,570 --> 00:10:01,890
And it's at that point when we can no longer type anything into our program and our code ends and we'd

108
00:10:01,890 --> 00:10:04,820
have to run the whole thing again in order to use it again.

109
00:10:06,370 --> 00:10:14,350
So at the moment, the part of our code that actually needs to be repeated if the user wanted to restart

110
00:10:14,350 --> 00:10:16,850
it is actually just this part.

111
00:10:17,290 --> 00:10:21,070
So the first thing I'm going to do is I'm actually going to shrink it down a bit.

112
00:10:21,370 --> 00:10:28,390
And now all we're left with is 1, 2, 3, 4, 5 lines of code which needs repeat if

113
00:10:28,390 --> 00:10:31,670
we're going to get the user to reuse the Caesar cipher.

114
00:10:32,350 --> 00:10:37,260
What if we go ahead and indent this and put it inside a while loop?

115
00:10:37,780 --> 00:10:40,990
What are we going to check to make sure that our loop continues?

116
00:10:41,500 --> 00:10:48,440
Well, we could create a flag called should_continue and we start this out to be true.

117
00:10:49,060 --> 00:10:56,320
And then while this is still true, we continue to keep looping through this so that the user gets to

118
00:10:56,530 --> 00:11:02,180
input a new direction, new text, new shift and call the Caesar cipher on that again.

119
00:11:02,980 --> 00:11:04,970
When should we stop it though?

120
00:11:04,990 --> 00:11:12,340
Well, if at the end of deciphering or encoding their message, we ask them a question, something like

121
00:11:12,340 --> 00:11:12,670
type

122
00:11:12,670 --> 00:11:13,060
yes

123
00:11:13,060 --> 00:11:15,580
if you want to go again or type no

124
00:11:15,580 --> 00:11:19,810
if you don't want to, we could put that inside an input.

125
00:11:20,900 --> 00:11:28,580
Like so. And we can save their answer inside a variable. Let's just call that result. So now we can

126
00:11:28,580 --> 00:11:30,040
use an if statement to check

127
00:11:30,050 --> 00:11:33,500
well, if the results is equal to no,

128
00:11:33,890 --> 00:11:39,830
so when they actually want to stop and they want to exit the while loop, well, at this point we can

129
00:11:39,830 --> 00:11:40,430
switch

130
00:11:40,430 --> 00:11:42,730
that should_continue to false.

131
00:11:43,220 --> 00:11:48,740
And once that's done, then this means that this while loop, when it checks it, this is going to be

132
00:11:48,740 --> 00:11:51,710
false and it will no longer continue.

133
00:11:52,280 --> 00:11:56,900
Just as a final touch, we can even tell the user goodbye

134
00:11:58,330 --> 00:12:00,550
so that they know that the program has ended.

135
00:12:01,270 --> 00:12:05,980
Now let's go ahead and run our code and see if it works.

136
00:12:07,840 --> 00:12:15,250
All right, so let's first encode something. And now I can type yes to go again, and you can see it's

137
00:12:15,250 --> 00:12:17,470
allowing me to type encode or decode.

138
00:12:17,770 --> 00:12:19,540
So let's encode something else.

139
00:12:22,740 --> 00:12:28,050
Now, finally, if I decided I've had enough, I'm done with encoding and decoding, I'm going to type

140
00:12:28,050 --> 00:12:28,550
no.

141
00:12:28,560 --> 00:12:34,620
It tells me goodbye and I can now see my prompt, which means it's actually exited out of the program

142
00:12:34,620 --> 00:12:37,830
and I would need to run it again in order to restart it.

143
00:12:38,520 --> 00:12:39,720
Did you manage to get that right?

144
00:12:40,560 --> 00:12:47,220
If you want to take a look at the completed code, then you can head over to caesar-cipher-4-end and

145
00:12:47,220 --> 00:12:49,640
you will find the same code as I've got here.

146
00:12:50,130 --> 00:12:56,020
And of course, feel free to play around with the code, modify it and make this program your own.

147
00:12:56,040 --> 00:13:01,890
So if you wanted to add some extra features, if you wanted to make sure that the numbers and symbols

148
00:13:01,890 --> 00:13:06,870
are all being encrypted and any sort of bells and whistles and anything else that you can think of,

149
00:13:07,200 --> 00:13:08,820
this is the time to do it now.

150
00:13:09,360 --> 00:13:15,360
But once you're done, be sure to get some rest, because tomorrow is yet another packed session where

151
00:13:15,360 --> 00:13:18,540
we're going to talk more about Python dictionaries.

152
00:13:18,900 --> 00:13:23,180
For all of that and more, I bid you good night and I'll see you tomorrow.

