Instructor: Let's go back to our example of our game
where we have a user login
and they have to provide their age.
Remember, if I click "run,"
it's going to ask me, "Hey, what's your age?"
And if I give it something that it doesn't like,
it's going to keep asking me
until I provide a proper value.
There's one other piece to the try/except lock
that we haven't covered.
So we know that we have "try,"
we have "except,"
and then we have "else."
But there's also another thing called "finally,"
and finally runs at the end
after everything has been executed.
Let me show you.
If I do print, let's say, "Okay, I'm finally done."
Let's see what happens.
If I click run here and I give it a proper age
I get "thank you" because my try block succeeded.
Nothing has failed.
I go to the else and I say "thank you."
And now I break out of the while loop,
but finally says,
"Hey, no matter what,
at the end of it all, I want you to finally do something."
Let's see this in action
if I do something like zero.
I get, "Please enter age higher than zero."
But then also still get, "Okay, I'm finally done."
What if I enter gibberish?
I get, "Hey, please enter a number,"
but also, "Okay, I'm finally done."
So finally runs regardless at the end of everything.
Why is this useful?
Maybe you're working on a game server
and you wanna make sure that you log out
any activity on the server.
So, let's say a user tries to log in.
Even if they try to enter the wrong information,
we want to log that activity,
so that maybe we can detect
that there's people trying to break our program,
or just to have our records that,
"Hey, this user has tried to log in 10 times."
So finally is very useful.
Now let me ask you a question.
What happens if I do something like this?
Let's say I have here "continue,"
and maybe another break statement here,
and then maybe inside of here,
I'm also going to have a print,
"Can you hear me?"
What happens if I do something like this?
Pause the video if you need to.
Otherwise, I'm going to hit "run,"
and see what's going to happen.
What is going to happen if I enter zero?
Ready?
I get, "Please enter age higher than zero,"
because I get a ZeroDivisionError.
It catches here, and then we break out of the loop.
The finally gets run, but print never runs because,
well, we break out of the loop.
And remember, this is surrounded by the loop.
Let me remove this so we can see it better.
Okay, what happens if I do something like this?
What if we run the program again
and this time give it gibberish?
Let's find out.
I get, "Please enter a number. Okay, I'm finally done,"
and then, "What's your age?"
So what happened here?
Well, we tried this,
we got an error, a value error.
It says, "Hey, please enter a number,"
and then it's going to continue.
Now continue instead of break
doesn't break out of the loop, out of the while loop.
Instead it comes back to the top.
So it's going to try again.
This part, "What's your age?"
Okay, so what if we finally try to log in properly
and give it, "Hey, we're 18."
What happens then?
There you go.
I get, "Thank you,"
because we've tried it, everything works.
So, no exception.
So we are gonna get "thank you"
and then we break out of the loop.
So once again, this print never gets printed,
"Can you hear me?"
But then finally, "Okay, finally is done."
If we didn't have this break statement here
and I click "run,"
and give it "18," oh boy,
"Finally I can hear you."
This print finally gets printed
because we don't break out of here.
We don't break out of here, and then we run this line.
Hopefully that makes sense.
That was a bit of a trick question
but you're starting to get the hang of it.
I'll see you in the next video.
Bye, bye.