Instructor: In this exercise,
we're going to bake some pies.
If you take a look at the starting code,
you'll see that we're taking the input as a list of fruits
and we're putting it into our code,
and on line 11, we call the function make pie
and tell it which pie out of our list of fruits to make.
So if make pie zero, then that would be to make apple pie,
make pie one, that should be a pear pie,
make pie two, that should be an orange pie.
Now, unfortunately, we are providing a index
that is way out of range of our list of fruits.
It's like we're trying to make a pie
with a fruit type that doesn't exist.
This normally would crash our code,
and indeed, if you just try running the code as it is
with the starting code,
you will see the errors in the output area.
Your job is to update the make pie function
so that in the case where the user
does something silly like this on line 11
where they try to pick a fruit that does not exist
in our list of fruits,
then instead of crashing the program,
we're just going to print "fruit pie".
So we don't know what fruit you want,
we're just gonna tell you we just made some fruit pie
and tell the user to go away.
But this is a really important skill,
the way that we can handle errors
instead of allowing it to crash our application.
Take a look in the description pane,
look at the example input
and what the example output is expected,
and write some code to catch this exception.
Have a go now.
The first thing we do is using a try block,
we catch the line of code
that is potentially going to cause us an error,
and in this case, it is the line on line seven
where we try to access the list of fruits
at an index that may or may not exist.
When we call line 13, make pie four,
we pass four into line seven
and we try to access our list of fruits at index four.
So that obviously doesn't exist.
Index three doesn't even exist.
There's only three items in our list.
So this line, line seven,
is the one that causes the index error,
and that's the one that we are penning in
in this safe environment under try.
The next part is where we have our exception.
We know from running the code,
the type of exception we get is an index error,
and this is the exception that we wanna handle in this case.
Now, there can be many, many other types
of errors and exceptions.
We don't want to handle all of them in this way
because there might be things
that we haven't even predicted that's wrong.
The only thing that we know that could go wrong
that we wanna handle is the index error.
So we have except index error,
and when this error does happen,
we're going to print fruit pie
instead of crashing our application.
Finally, we have an else block
that catches the case where there are no exceptions,
and in this case, we're just going to do our usual,
which is to print the name of the fruit from the list
that is picked plus the word pie.
And this block of code now in make pie
is now safe against the potential index error.
If the user changes line 13
to say make pie one, make pie two,
it's going to function as expected,
printing out the name of the fruit and the word pie,
but if they make a mistake
and happen to go out of the range, of the list,
then we're just going to print fruit pie.
There's no crashes, there's no alarm bells,
everything is all safe and all handled,
which is the point of our exception handling.
And this is a really important skill,
if not a little bit neglected by developers
because it's not fun thinking about edge cases
and handling them each time.
But this is an important skill on your road
to becoming a professional developer.