1
00:00:00,180 --> 00:00:02,340
Now for the final coding exercise,

2
00:00:02,370 --> 00:00:06,060
we're actually going to jump into a previous project that we created.

3
00:00:06,480 --> 00:00:10,230
This is the NATO alphabet project that we created a few days ago.

4
00:00:10,770 --> 00:00:12,150
And it's pretty simple.

5
00:00:12,300 --> 00:00:16,590
All it does is it takes the data that's inside this nato_phonetic_alphabet.

6
00:00:16,590 --> 00:00:17,423
csv,

7
00:00:17,730 --> 00:00:22,730
and it uses that to create a dictionary so that we have each letter as the key

8
00:00:25,230 --> 00:00:30,230
and the actual phonetic alphabet as the value for each of those keys.

9
00:00:32,189 --> 00:00:34,140
Now, if I go ahead and hit run,

10
00:00:35,250 --> 00:00:39,240
you can see that the entire phonetic alphabet dictionary is printed here

11
00:00:39,510 --> 00:00:42,570
and this is the structure of that dictionary. Now,

12
00:00:42,570 --> 00:00:47,570
the next thing it asks us is to enter a word and it takes that word and creates

13
00:00:49,140 --> 00:00:54,140
a new list using list comprehension that matches each of the letters to the

14
00:00:55,860 --> 00:01:00,420
correct phonetic alphabet. So Angela becomes Alpha November Golf Echo

15
00:01:00,420 --> 00:01:01,320
Lima Alpha.

16
00:01:01,680 --> 00:01:05,970
So you should remember all of this from creating this project a few days ago.

17
00:01:06,600 --> 00:01:06,870
Now,

18
00:01:06,870 --> 00:01:11,870
what we're going to do is we're going to update and refresh this project so that

19
00:01:12,780 --> 00:01:15,840
it's even better than before. Now,

20
00:01:15,870 --> 00:01:20,870
if you don't have access to the final project of this NATO alphabet project,

21
00:01:21,330 --> 00:01:25,110
then don't worry. Just head over to the course resources for today's lessons

22
00:01:25,440 --> 00:01:29,880
and you'll find a starting file that matches what you see here precisely.

23
00:01:30,990 --> 00:01:35,990
Now your job is to address a particular situation because we're relying on the

24
00:01:37,080 --> 00:01:39,630
user to enter a word.

25
00:01:40,260 --> 00:01:44,670
Then if they enter something that is actually not in this dictionary,

26
00:01:44,940 --> 00:01:48,010
let's say they entered just a number, 1234,

27
00:01:48,510 --> 00:01:52,740
then we will get a crash and we get a key error in this case.

28
00:01:53,220 --> 00:01:58,220
Now what we want to happen instead is for us to enter a word and we enter

29
00:01:58,440 --> 00:02:01,260
something that's complete nonsense. We hit enter.

30
00:02:01,410 --> 00:02:05,190
And it tells us that the sorry, only letters in the alphabet please.

31
00:02:05,520 --> 00:02:08,940
And then it goes back to give us that same prompt again.

32
00:02:09,449 --> 00:02:12,540
And if we keep refusing and we keep typing numbers,

33
00:02:12,840 --> 00:02:17,340
then it's going to continuously give us this feedback until we actually end up

34
00:02:17,370 --> 00:02:20,970
typing something that contains only letters in the alphabet

35
00:02:21,390 --> 00:02:24,660
and it actually generates the phonetic alphabet for us.

36
00:02:25,230 --> 00:02:29,160
This is the goal that we're trying to achieve. And in order to do this,

37
00:02:29,280 --> 00:02:32,400
you're going to have to use your skills of exception handling.

38
00:02:33,000 --> 00:02:35,160
Have a think about how you might achieve this,

39
00:02:35,520 --> 00:02:38,190
pause the video and complete this challenge.

40
00:02:41,520 --> 00:02:46,350
Now that we know what we're aiming for, the next step is to actually try to get

41
00:02:46,350 --> 00:02:50,670
there. Now we know that the part of the code that is prone to error

42
00:02:51,030 --> 00:02:54,780
or can occasionally be problematic is this line right here,

43
00:02:55,350 --> 00:03:00,070
because we're going through this phonetic dictionary and we're passing it a key and

44
00:03:00,070 --> 00:03:04,480
that key depends entirely on what the user puts into this input.

45
00:03:04,960 --> 00:03:09,760
So that means if they put something that is a number and it doesn't exist inside

46
00:03:09,760 --> 00:03:12,700
the phonetic dictionary, that's when we get our key error.

47
00:03:13,270 --> 00:03:17,500
So this is the line that we want to put behind a try block.

48
00:03:19,000 --> 00:03:19,930
Like this.

49
00:03:20,560 --> 00:03:25,560
And when we try out this line and if it actually fails and we end up with an

50
00:03:26,050 --> 00:03:28,840
exception, then we want to catch that exception.

51
00:03:29,140 --> 00:03:34,030
But we want to catch the specific exception, which is the key error. Now,

52
00:03:34,030 --> 00:03:38,470
what do we want to do when we actually have an exception when that key doesn't

53
00:03:38,470 --> 00:03:43,180
exist? Well, it means that the user entered something into the input

54
00:03:43,270 --> 00:03:46,390
that was not a letter from the alphabet.

55
00:03:46,930 --> 00:03:48,700
So we're going to give them some feedback.

56
00:03:48,730 --> 00:03:53,020
We're simply going to use a print statement to print 'Sorry,

57
00:03:53,080 --> 00:03:57,850
only letters in the alphabet please.' Hopefully that way they will learn

58
00:03:57,970 --> 00:04:02,590
and they will only write letters instead of trying to type other things like

59
00:04:02,590 --> 00:04:05,950
symbols or numbers. Now,

60
00:04:05,980 --> 00:04:09,430
if everything went well, so in the else case,

61
00:04:09,460 --> 00:04:12,430
then we actually want to print out the output list.

62
00:04:12,700 --> 00:04:14,500
Because if this line succeeded,

63
00:04:14,800 --> 00:04:19,750
then we will have that to print out to the user. Now,

64
00:04:19,779 --> 00:04:23,560
the only problem here is if we run the code as it is right now,

65
00:04:23,980 --> 00:04:27,580
we can enter a word, so let's try something that's not a word,

66
00:04:28,150 --> 00:04:31,660
and we get that feedback, but our code ends.

67
00:04:32,110 --> 00:04:37,110
So if we want our code to repeat so that we ask the user for a word again,

68
00:04:37,990 --> 00:04:42,160
then we're going to have to have some sort of mechanism like a loop or a

69
00:04:42,160 --> 00:04:44,530
function. So I think in this case,

70
00:04:44,530 --> 00:04:49,530
the best thing to do is actually to create a function which I'll call generate_

71
00:04:51,310 --> 00:04:52,180
phonetic.

72
00:04:53,230 --> 00:04:57,550
And this function is going to contain all of this code.

73
00:04:58,090 --> 00:05:00,310
So in order to run this function,

74
00:05:00,400 --> 00:05:03,640
we of course have to call it outside of the function.

75
00:05:03,910 --> 00:05:06,580
So once the code runs down to here,

76
00:05:06,910 --> 00:05:11,110
it sees this line and it will run all the code inside this function.

77
00:05:11,860 --> 00:05:16,860
But what we also want to do is if there was the case of a key error where the user

78
00:05:18,400 --> 00:05:19,750
typed in something wrong,

79
00:05:20,020 --> 00:05:23,440
then we want to give them the opportunity to type in something again.

80
00:05:23,560 --> 00:05:26,950
So we want to be able to run this line of code again.

81
00:05:27,730 --> 00:05:32,730
The easiest way of doing that is to simply call this function right here.

82
00:05:33,550 --> 00:05:37,660
So that way, if they typed in something wrong, we give them some feedback

83
00:05:37,930 --> 00:05:40,870
and then we go to the very start of this function once again,

84
00:05:41,200 --> 00:05:44,980
and we get them to type in a new word using this input.

85
00:05:45,580 --> 00:05:50,580
And then we can see if that was valid or if it was not and deal with that

86
00:05:50,830 --> 00:05:54,220
accordingly. So now when we run our code,

87
00:05:54,310 --> 00:05:56,290
it won't generate any errors,

88
00:05:56,560 --> 00:06:01,220
but instead it gives me some feedback and tells me to enter another word.

89
00:06:02,300 --> 00:06:06,290
So this is more of a real-life implementation of error handling,

90
00:06:06,620 --> 00:06:11,620
making sure that we are validating and checking the user's inputs and addressing

91
00:06:12,410 --> 00:06:15,980
these situations ahead of time using exception handling.

92
00:06:17,390 --> 00:06:21,530
Now that we've taken a good look at exception handling, in the next lesson

93
00:06:21,650 --> 00:06:25,940
we're going to go back to our password manager project and use the skills that

94
00:06:25,940 --> 00:06:28,190
we've learned to improve it even further.

95
00:06:28,880 --> 00:06:31,280
So, for all of that and more, I'll see you on the next lesson.

