Now for the final coding exercise,
we're actually going to jump into a previous project that we created.
This is the NATO alphabet project that we created a few days ago.
And it's pretty simple.
All it does is it takes the data that's inside this nato_phonetic_alphabet.
csv,
and it uses that to create a dictionary so that we have each letter as the key
and the actual phonetic alphabet as the value for each of those keys.
Now, if I go ahead and hit run,
you can see that the entire phonetic alphabet dictionary is printed here
and this is the structure of that dictionary. Now,
the next thing it asks us is to enter a word and it takes that word and creates
a new list using list comprehension that matches each of the letters to the
correct phonetic alphabet. So Angela becomes Alpha November Golf Echo
Lima Alpha.
So you should remember all of this from creating this project a few days ago.
Now,
what we're going to do is we're going to update and refresh this project so that
it's even better than before. Now,
if you don't have access to the final project of this NATO alphabet project,
then don't worry. Just head over to the course resources for today's lessons
and you'll find a starting file that matches what you see here precisely.
Now your job is to address a particular situation because we're relying on the
user to enter a word.
Then if they enter something that is actually not in this dictionary,
let's say they entered just a number, 1234,
then we will get a crash and we get a key error in this case.
Now what we want to happen instead is for us to enter a word and we enter
something that's complete nonsense. We hit enter.
And it tells us that the sorry, only letters in the alphabet please.
And then it goes back to give us that same prompt again.
And if we keep refusing and we keep typing numbers,
then it's going to continuously give us this feedback until we actually end up
typing something that contains only letters in the alphabet
and it actually generates the phonetic alphabet for us.
This is the goal that we're trying to achieve. And in order to do this,
you're going to have to use your skills of exception handling.
Have a think about how you might achieve this,
pause the video and complete this challenge.
Now that we know what we're aiming for, the next step is to actually try to get
there. Now we know that the part of the code that is prone to error
or can occasionally be problematic is this line right here,
because we're going through this phonetic dictionary and we're passing it a key and
that key depends entirely on what the user puts into this input.
So that means if they put something that is a number and it doesn't exist inside
the phonetic dictionary, that's when we get our key error.
So this is the line that we want to put behind a try block.
Like this.
And when we try out this line and if it actually fails and we end up with an
exception, then we want to catch that exception.
But we want to catch the specific exception, which is the key error. Now,
what do we want to do when we actually have an exception when that key doesn't
exist? Well, it means that the user entered something into the input
that was not a letter from the alphabet.
So we're going to give them some feedback.
We're simply going to use a print statement to print 'Sorry,
only letters in the alphabet please.' Hopefully that way they will learn
and they will only write letters instead of trying to type other things like
symbols or numbers. Now,
if everything went well, so in the else case,
then we actually want to print out the output list.
Because if this line succeeded,
then we will have that to print out to the user. Now,
the only problem here is if we run the code as it is right now,
we can enter a word, so let's try something that's not a word,
and we get that feedback, but our code ends.
So if we want our code to repeat so that we ask the user for a word again,
then we're going to have to have some sort of mechanism like a loop or a
function. So I think in this case,
the best thing to do is actually to create a function which I'll call generate_
phonetic.
And this function is going to contain all of this code.
So in order to run this function,
we of course have to call it outside of the function.
So once the code runs down to here,
it sees this line and it will run all the code inside this function.
But what we also want to do is if there was the case of a key error where the user
typed in something wrong,
then we want to give them the opportunity to type in something again.
So we want to be able to run this line of code again.
The easiest way of doing that is to simply call this function right here.
So that way, if they typed in something wrong, we give them some feedback
and then we go to the very start of this function once again,
and we get them to type in a new word using this input.
And then we can see if that was valid or if it was not and deal with that
accordingly. So now when we run our code,
it won't generate any errors,
but instead it gives me some feedback and tells me to enter another word.
So this is more of a real-life implementation of error handling,
making sure that we are validating and checking the user's inputs and addressing
these situations ahead of time using exception handling.
Now that we've taken a good look at exception handling, in the next lesson
we're going to go back to our password manager project and use the skills that
we've learned to improve it even further.
So, for all of that and more, I'll see you on the next lesson.