WEBVTT

0
00:00.110 --> 00:02.300
Now we're really close to the finish line.

1
00:02.300 --> 00:06.200
I know today is quite a long day, but just bear in there,

2
00:06.200 --> 00:08.300
you're going to get there really, really soon.

3
00:08.300 --> 00:13.310
So the last part of our Caesar Cipher just has three TODOs.

4
00:13.310 --> 00:18.980
One is we need to import and print the logo from this file, art.py.

5
00:19.220 --> 00:25.790
And then we have to figure out, well, what happens if the user enters a number or space.

6
00:25.790 --> 00:33.290
So if you take a look at the final, demo and we were to write a message, say "hello," and then we add

7
00:33.290 --> 00:41.600
some numbers or some symbols, we don't want those to be lost when we encode the message,

8
00:41.600 --> 00:42.050
right?

9
00:42.050 --> 00:48.410
So what we want instead is we only want to encode the alphabetical letters.

10
00:48.410 --> 00:55.820
And then for anything that is not within the alphabet, we just keep it as it is and add it to the final

11
00:55.820 --> 00:57.380
encoded result.

12
00:57.410 --> 01:03.440
This way we can encode and decode and keep any of these characters the user types in.

13
01:03.440 --> 01:12.080
And then finally we need a way for our program to rerun once the user is done with encoding or decoding.

14
01:13.220 --> 01:17.690
So that way we ask them, "Type 'yes' if you want to go again.

15
01:17.690 --> 01:19.270
Otherwise type 'no'."

16
01:19.270 --> 01:24.040
And if they type yes then they get to encode_or_decode again.

17
01:24.040 --> 01:28.720
And once they're actually done then they could type 'no',

18
01:28.720 --> 01:33.220
and the program just says, 'Goodbye' like a good little robot.

19
01:33.220 --> 01:35.500
So those are the three TODOs,

20
01:35.500 --> 01:37.660
have a look in the Description box,

21
01:37.660 --> 01:44.710
have a look at the hints if you need them, and go ahead and pause the video and complete the final

22
01:44.710 --> 01:47.260
part of the Caesar Cipher project.

23
01:53.680 --> 01:57.970
Okay, hopefully, it was straightforward and you're just here to check the solution.

24
01:57.970 --> 02:03.160
If not, don't worry, I'm going to walk through everything and explain everything anyways, so hopefully

25
02:03.160 --> 02:05.140
it'll be a lot more clear afterwards.

26
02:05.140 --> 02:07.300
So TODO one is pretty simple,

27
02:07.300 --> 02:14.830
All we need to do is import the module art and then just simply print(art.logo), which is the name

28
02:14.830 --> 02:16.540
that we've got in here.

29
02:16.540 --> 02:18.160
That's the name of the variable.

30
02:18.160 --> 02:26.020
And that's going to print out that Caesar Cipher logo with ASCII art every time we start our program.

31
02:26.020 --> 02:29.860
Now you can also do the previous from import,

32
02:29.860 --> 02:33.040
but this is also clear enough as well.

33
02:33.220 --> 02:40.480
Now TODO Number 2, we need to figure out what to do if the user enters a number, or symbol, or space,

34
02:40.480 --> 02:46.210
and how can we keep hold of it when they actually type that in.

35
02:46.210 --> 02:52.900
So let's think about where we would make that differentiation every time we loop through each of the

36
02:52.900 --> 02:59.560
letters, if that letter that we're looping through from the original_text happens to be not something

37
02:59.560 --> 03:05.260
that exists in this list of alphabet, then it's probably a symbol or a number or something that we

38
03:05.260 --> 03:07.090
can't shift very easily.

39
03:07.090 --> 03:13.990
So then we probably want to bypass the rest of this and do something separate.

40
03:14.890 --> 03:25.480
So what we can do is we can say if the letter is not in the alphabet list, then in that case, all

41
03:25.480 --> 03:31.900
we're going to do is simply take the output_text += the current letter.

42
03:31.900 --> 03:39.070
And this will add whatever letter it is that we need to skip into the output_text as its original format.

43
03:39.070 --> 03:46.140
But then we can have an else statement and have the rest of this carryout

44
03:46.140 --> 03:53.040
if this is not the case, which means the letter is in the alphabet and we can in fact shift it.

45
03:53.040 --> 03:59.010
Now, another way that we could do this is we could add a bunch of symbols and numbers to our alphabet

46
03:59.010 --> 04:02.340
list, and instead of calling it alphabet, call it something else,

47
04:02.340 --> 04:04.740
and that way we can be more inclusive.

48
04:04.740 --> 04:09.330
But it's quite hard to include all of the symbols and all of the numbers.

49
04:09.330 --> 04:15.720
And just to keep it simple for this practice exercise, this is probably the most straightforward solution.

50
04:16.260 --> 04:19.530
So now we're on to TODO Number 3,

51
04:19.530 --> 04:22.860
can you figure out a way to restart the Cipher program.

52
04:22.860 --> 04:28.470
So as previous, the way that we need to do this is to use a while loop.

53
04:28.470 --> 04:34.170
So if we have some sort of variable let's call it should_continue,

54
04:34.740 --> 04:42.510
and we set it to true because normally while the program is going, it should in fact continue asking

55
04:42.510 --> 04:45.120
the user, do you want to go again?

56
04:45.120 --> 04:46.380
Do you want to go again?

57
04:46.380 --> 04:49.530
But sometimes the user will say, no.

58
04:49.530 --> 04:54.690
So let's go ahead and create a while loop that checks for this variable.

59
04:54.690 --> 05:00.960
And if it is the case, then we're going to keep running all of these inputs, asking them direction,

60
05:00.960 --> 05:04.290
text, shift and also running our ceasar() function.

61
05:04.290 --> 05:12.060
But then at the end of all of that, once we've given them the encoded_or_decoded text and that caesar()

62
05:12.060 --> 05:19.860
function has come to an end, then the next thing we're going to do is we're going to check with them

63
05:19.860 --> 05:22.020
whether if they want to go again.

64
05:22.020 --> 05:26.430
So I'm going to copy this message and add it into an input.

65
05:27.330 --> 05:38.280
So in this input, I'm going to go ahead and change it to .lower() to make sure that we've got

66
05:38.280 --> 05:45.660
the...whatever it is they type capital, or no capital set to the same thing so that we can check it more

67
05:45.660 --> 05:46.440
easily.

68
05:46.440 --> 05:55.890
And I'm also just going to add a \n so that our cursor goes on to the next line when they do

69
05:55.890 --> 06:00.510
have to type, so that its cursor here rather than cursor here.

70
06:00.840 --> 06:05.850
Now finally I'm going to save that input into a variable called restart.

71
06:05.850 --> 06:14.600
And now we can check the value of that restart. If restart is equal to "no",

72
06:14.630 --> 06:20.600
well, in that case, we can change our should continue to False.

73
06:20.600 --> 06:25.070
And then we can print ("Goodbye").

74
06:25.070 --> 06:28.550
And if the restart is any other value.

75
06:28.550 --> 06:30.680
So 'yes', or capital

76
06:30.680 --> 06:34.550
'Yes', or whichever then this if statement is skipped,

77
06:34.550 --> 06:38.660
and our while loop loops back up from the start.

78
06:38.660 --> 06:48.440
So that is how we can make sure that our code continues asking the user for a message and their encoding

79
06:48.440 --> 06:49.580
or decoding.

80
06:49.580 --> 06:53.630
And it will continue forever until they basically say, 'no'.

81
06:53.720 --> 07:01.580
Now, if you've been following my code all along, I want you to try and test your code right now repeatedly,

82
07:01.580 --> 07:05.300
and see if you can spot a crucial bug.

83
07:05.330 --> 07:12.380
Now it relates to this caesar() function, and I want you to see if you can, through testing, figure

84
07:12.380 --> 07:14.960
out what is wrong and solve the issue.

85
07:14.990 --> 07:16.520
So pause the video now.

86
07:18.860 --> 07:21.110
So did you manage to find the problem?

87
07:21.140 --> 07:22.610
Here, let me show you.

88
07:22.610 --> 07:31.730
So if I go ahead and start encoding and we decide to type a and b, and then type the shift to go up by

89
07:31.730 --> 07:35.060
2, so that becomes cd, which is great.

90
07:35.060 --> 07:36.440
So let's go again.

91
07:36.440 --> 07:45.470
And let's now type decode and again use the previous cyber_text which is CD.

92
07:45.800 --> 07:50.090
And if we take our cipher_text and shift it down by 2,

93
07:50.120 --> 07:57.860
we should expect ab, which is our original message which we encoded up here, but instead we get an

94
07:57.860 --> 07:58.340
f.

95
07:58.340 --> 08:00.530
So what is going on?

96
08:00.560 --> 08:11.120
Well, if you debug either using the debugger or your own knowledge, you'll find that this line here

97
08:11.120 --> 08:12.650
is the culprit.

98
08:13.220 --> 08:19.730
We're determining the shift-amount by shifting it back to the negative,

99
08:19.730 --> 08:22.370
and that's what happens when we do -1.

100
08:22.490 --> 08:30.860
But this is inside the for loop, which means that it's going to happen and flip every single time the

101
08:30.860 --> 08:32.120
for loop runs.

102
08:32.120 --> 08:40.690
So in order to fix this, what we need to do is just to take it out of the for loop and then put it

103
08:40.690 --> 08:42.340
above the for loop.

104
08:42.340 --> 08:48.280
And now if we retest, you'll see we've managed to solve that problem.

105
08:48.490 --> 08:54.220
So going up works, and going down also works.

106
08:54.910 --> 09:01.030
So that is all there is to it to this three-part Caesar Cipher project.

107
09:01.030 --> 09:07.450
And of course, as always with all of these projects, feel free to add modifications, tackle any edge

108
09:07.450 --> 09:12.460
cases, there might be things that I haven't thought of, that you've thought of, it might be improvements

109
09:12.460 --> 09:13.420
you want to make.

110
09:13.450 --> 09:18.940
The program is yours, so you can do whatever it is you want to do to improve it.

111
09:18.940 --> 09:26.590
And hopefully in the process, you've managed to solidify and revise some of the topics that we've learned

112
09:26.590 --> 09:29.170
in today and previous lessons.

113
09:29.170 --> 09:32.440
And if it was really, really hard, then that's a good thing.

114
09:32.440 --> 09:37.540
That means you really struggled and you're building those programming muscles, right?

115
09:37.540 --> 09:41.200
There's no point going to the gym and lifting one kilogram weights.

116
09:41.200 --> 09:44.530
It's not going to make you very muscular.

117
09:44.530 --> 09:47.650
We got to push ourselves and this is okay.

118
09:47.650 --> 09:51.940
So I hope you had fun with me today and I look forward to seeing you tomorrow.