WEBVTT

0
00:00.180 --> 00:02.340
Now for the final coding exercise,

1
00:02.370 --> 00:06.060
we're actually going to jump into a previous project that we created.

2
00:06.480 --> 00:10.230
This is the NATO alphabet project that we created a few days ago.

3
00:10.770 --> 00:12.150
And it's pretty simple.

4
00:12.300 --> 00:16.590
All it does is it takes the data that's inside this nato_phonetic_alphabet.

5
00:16.590 --> 00:17.423
csv,

6
00:17.730 --> 00:22.730
and it uses that to create a dictionary so that we have each letter as the key

7
00:25.230 --> 00:30.230
and the actual phonetic alphabet as the value for each of those keys.

8
00:32.190 --> 00:34.140
Now, if I go ahead and hit run,

9
00:35.250 --> 00:39.240
you can see that the entire phonetic alphabet dictionary is printed here

10
00:39.510 --> 00:42.570
and this is the structure of that dictionary. Now,

11
00:42.570 --> 00:47.570
the next thing it asks us is to enter a word and it takes that word and creates

12
00:49.140 --> 00:54.140
a new list using list comprehension that matches each of the letters to the

13
00:55.860 --> 01:00.420
correct phonetic alphabet. So Angela becomes Alpha November Golf Echo

14
01:00.420 --> 01:01.320
Lima Alpha.

15
01:01.680 --> 01:05.970
So you should remember all of this from creating this project a few days ago.

16
01:06.600 --> 01:06.870
Now,

17
01:06.870 --> 01:11.870
what we're going to do is we're going to update and refresh this project so that

18
01:12.780 --> 01:15.840
it's even better than before. Now,

19
01:15.870 --> 01:20.870
if you don't have access to the final project of this NATO alphabet project,

20
01:21.330 --> 01:25.110
then don't worry. Just head over to the course resources for today's lessons

21
01:25.440 --> 01:29.880
and you'll find a starting file that matches what you see here precisely.

22
01:30.990 --> 01:35.990
Now your job is to address a particular situation because we're relying on the

23
01:37.080 --> 01:39.630
user to enter a word.

24
01:40.260 --> 01:44.670
Then if they enter something that is actually not in this dictionary,

25
01:44.940 --> 01:48.010
let's say they entered just a number, 1234,

26
01:48.510 --> 01:52.740
then we will get a crash and we get a key error in this case.

27
01:53.220 --> 01:58.220
Now what we want to happen instead is for us to enter a word and we enter

28
01:58.440 --> 02:01.260
something that's complete nonsense. We hit enter.

29
02:01.410 --> 02:05.190
And it tells us that the sorry, only letters in the alphabet please.

30
02:05.520 --> 02:08.940
And then it goes back to give us that same prompt again.

31
02:09.450 --> 02:12.540
And if we keep refusing and we keep typing numbers,

32
02:12.840 --> 02:17.340
then it's going to continuously give us this feedback until we actually end up

33
02:17.370 --> 02:20.970
typing something that contains only letters in the alphabet

34
02:21.390 --> 02:24.660
and it actually generates the phonetic alphabet for us.

35
02:25.230 --> 02:29.160
This is the goal that we're trying to achieve. And in order to do this,

36
02:29.280 --> 02:32.400
you're going to have to use your skills of exception handling.

37
02:33.000 --> 02:35.160
Have a think about how you might achieve this,

38
02:35.520 --> 02:38.190
pause the video and complete this challenge.

39
02:41.520 --> 02:46.350
Now that we know what we're aiming for, the next step is to actually try to get

40
02:46.350 --> 02:50.670
there. Now we know that the part of the code that is prone to error

41
02:51.030 --> 02:54.780
or can occasionally be problematic is this line right here,

42
02:55.350 --> 03:00.070
because we're going through this phonetic dictionary and we're passing it a key and

43
03:00.070 --> 03:04.480
that key depends entirely on what the user puts into this input.

44
03:04.960 --> 03:09.760
So that means if they put something that is a number and it doesn't exist inside

45
03:09.760 --> 03:12.700
the phonetic dictionary, that's when we get our key error.

46
03:13.270 --> 03:17.500
So this is the line that we want to put behind a try block.

47
03:19.000 --> 03:19.930
Like this.

48
03:20.560 --> 03:25.560
And when we try out this line and if it actually fails and we end up with an

49
03:26.050 --> 03:28.840
exception, then we want to catch that exception.

50
03:29.140 --> 03:34.030
But we want to catch the specific exception, which is the key error. Now,

51
03:34.030 --> 03:38.470
what do we want to do when we actually have an exception when that key doesn't

52
03:38.470 --> 03:43.180
exist? Well, it means that the user entered something into the input

53
03:43.270 --> 03:46.390
that was not a letter from the alphabet.

54
03:46.930 --> 03:48.700
So we're going to give them some feedback.

55
03:48.730 --> 03:53.020
We're simply going to use a print statement to print 'Sorry,

56
03:53.080 --> 03:57.850
only letters in the alphabet please.' Hopefully that way they will learn

57
03:57.970 --> 04:02.590
and they will only write letters instead of trying to type other things like

58
04:02.590 --> 04:05.950
symbols or numbers. Now,

59
04:05.980 --> 04:09.430
if everything went well, so in the else case,

60
04:09.460 --> 04:12.430
then we actually want to print out the output list.

61
04:12.700 --> 04:14.500
Because if this line succeeded,

62
04:14.800 --> 04:19.750
then we will have that to print out to the user. Now,

63
04:19.780 --> 04:23.560
the only problem here is if we run the code as it is right now,

64
04:23.980 --> 04:27.580
we can enter a word, so let's try something that's not a word,

65
04:28.150 --> 04:31.660
and we get that feedback, but our code ends.

66
04:32.110 --> 04:37.110
So if we want our code to repeat so that we ask the user for a word again,

67
04:37.990 --> 04:42.160
then we're going to have to have some sort of mechanism like a loop or a

68
04:42.160 --> 04:44.530
function. So I think in this case,

69
04:44.530 --> 04:49.530
the best thing to do is actually to create a function which I'll call generate_

70
04:51.310 --> 04:52.180
phonetic.

71
04:53.230 --> 04:57.550
And this function is going to contain all of this code.

72
04:58.090 --> 05:00.310
So in order to run this function,

73
05:00.400 --> 05:03.640
we of course have to call it outside of the function.

74
05:03.910 --> 05:06.580
So once the code runs down to here,

75
05:06.910 --> 05:11.110
it sees this line and it will run all the code inside this function.

76
05:11.860 --> 05:16.860
But what we also want to do is if there was the case of a key error where the user

77
05:18.400 --> 05:19.750
typed in something wrong,

78
05:20.020 --> 05:23.440
then we want to give them the opportunity to type in something again.

79
05:23.560 --> 05:26.950
So we want to be able to run this line of code again.

80
05:27.730 --> 05:32.730
The easiest way of doing that is to simply call this function right here.

81
05:33.550 --> 05:37.660
So that way, if they typed in something wrong, we give them some feedback

82
05:37.930 --> 05:40.870
and then we go to the very start of this function once again,

83
05:41.200 --> 05:44.980
and we get them to type in a new word using this input.

84
05:45.580 --> 05:50.580
And then we can see if that was valid or if it was not and deal with that

85
05:50.830 --> 05:54.220
accordingly. So now when we run our code,

86
05:54.310 --> 05:56.290
it won't generate any errors,

87
05:56.560 --> 06:01.220
but instead it gives me some feedback and tells me to enter another word.

88
06:02.300 --> 06:06.290
So this is more of a real-life implementation of error handling,

89
06:06.620 --> 06:11.620
making sure that we are validating and checking the user's inputs and addressing

90
06:12.410 --> 06:15.980
these situations ahead of time using exception handling.

91
06:17.390 --> 06:21.530
Now that we've taken a good look at exception handling, in the next lesson

92
06:21.650 --> 06:25.940
we're going to go back to our password manager project and use the skills that

93
06:25.940 --> 06:28.190
we've learned to improve it even further.

94
06:28.880 --> 06:31.280
So, for all of that and more, I'll see you on the next lesson.