Instructor: In this exercise,
we're going to write some exception handling code
to handle a different type of exception, a KeyError.
When you hear keys, you're probably thinking dictionaries
and you would be right.
So in this case, in the input area,
you can see we have a list
of dictionaries that contains some data
on some Facebook posts.
As you may know, Facebook posts can have likes,
they can have comments, they can have shares,
they can have lots of different attributes assigned to them
and this can be collected in a dictionary.
Now, the problem occurs on line seven of the starting code
where we try to access, in each of the dictionaries,
the posts, the number of likes that that post has.
Now, the problem here is that if you look
at the input or if you look at the Facebook posts
in a better structured way in the description pane,
you'll see that some of those posts,
some of those dictionaries don't have a value for likes.
Those posts didn't get liked at all.
They were hated on, or they had different reactions.
But either way, if we try to access a key
from a dictionary that doesn't exist, we'll end up
with a KeyError and you'll see
that if you try to run the code
without changing the starting code at all.
Now, the objective is for you to use what you've learned
about exception handling
to prevent the program from crashing.
And instead, we're going to print out the total number
of likes of this user's Facebook posts
and we're going to skip the ones where there are no likes
for that particular post.
Take a look at the description pane, read through it,
take a look at the starting code
and see if you can figure out
how to make all the tests pass in this exercise.
The first thing we do, as always,
is to put the problematic code inside a little jail,
a try jail.
And in this case, we have inside the for loop,
we have another block, which is the try block.
Inside the try block, we have our offending line of code,
total_likes equals total_likes plus post,
accessing each of those dictionaries,
which we've called posts and accessing it by the key likes.
Now, this is the line that we know causes the error
and this is the line that we're going to try to execute.
What happens if our try fails?
Well, we're going to add an accept block,
and we're going to define the accept
as a KeyError exception.
Writing accept KeyError: means that if a KeyError
does occur when we try to run line seven,
then well, action is going to skip that line, and instead,
we're going to run pass, which inside a loop
is going to skip over this line
and go to the next item in the loop.
So we're going to loop through all
of the dictionaries in our list called facebook_posts.
If one of them doesn't have any likes,
we pass on that one and we continue with the next one.
And then at the end, we end up accumulating our total number
of likes and we print it out,
and that is the solution to this challenge.
Exception handling is incredibly effective
and incredibly useful,
although it is sometimes neglected by developers
but by hopefully learning more exceptions and thinking
about exception handling using try/accept blocks,
you're getting to grips around a really key part
of development, which is error and exception handling.