1 00:00:00,300 --> 00:00:08,070 Let's go back to our example of our game, where we have a user log in and they have to provide their 2 00:00:08,070 --> 00:00:12,120 age, remember, if I click run, it's going to ask me, hey, what's your age? 3 00:00:12,450 --> 00:00:16,800 And if I give it something that it doesn't like, it's going to keep asking me until. 4 00:00:18,200 --> 00:00:19,640 I provide a proper value. 5 00:00:20,800 --> 00:00:26,700 There's one other piece to the try except block that we have uncovered, so we know that we have try, 6 00:00:27,490 --> 00:00:30,370 we have except and then we have else. 7 00:00:30,790 --> 00:00:36,160 But there's also another thing called finally and finally runs. 8 00:00:37,060 --> 00:00:42,100 At the end, after everything has been executed, let me show you if I do print. 9 00:00:43,150 --> 00:00:45,910 I say, OK, I'm finally done. 10 00:00:50,110 --> 00:00:55,050 Let's see what happens if I click run here and I give it a proper age. 11 00:00:56,300 --> 00:01:02,100 I get thank you because my tribe succeeded, nothing has failed. 12 00:01:02,420 --> 00:01:05,420 I go to the else and I say thank you. 13 00:01:06,900 --> 00:01:09,240 And now I break out of the. 14 00:01:10,250 --> 00:01:18,050 While loop, but finally says, hey, no matter what, at the end of it all, I want you to finally 15 00:01:18,050 --> 00:01:18,680 do something. 16 00:01:19,910 --> 00:01:22,640 Let's see this in action if I do something like zero. 17 00:01:23,850 --> 00:01:28,590 Again, please enter higher than zero, but then also still get. 18 00:01:28,620 --> 00:01:29,880 OK, I'm finally done. 19 00:01:30,390 --> 00:01:31,830 What if I enter a jibberish? 20 00:01:32,990 --> 00:01:42,110 I get hey, please enter a number, but also, OK, I'm finally done, so finally runs regardless at 21 00:01:42,110 --> 00:01:43,390 the end of everything. 22 00:01:45,120 --> 00:01:52,110 Why is this useful, maybe you're working on a game server and you want to make sure that you log out 23 00:01:52,110 --> 00:01:54,270 any activity on the server. 24 00:01:54,300 --> 00:01:59,890 So let's say a user tries to log in even if they try to enter the wrong information. 25 00:02:00,630 --> 00:02:06,480 We want to log that activity so that maybe we can detect that there's people trying to break our program 26 00:02:06,660 --> 00:02:10,880 or just to have our records that, hey, this user has tried to log in 10 times. 27 00:02:11,850 --> 00:02:13,890 So finally is very useful. 28 00:02:14,400 --> 00:02:17,040 Now, let me ask you a question. 29 00:02:17,820 --> 00:02:19,830 What happens if I do something like this? 30 00:02:20,520 --> 00:02:22,770 Let's say I have here continue. 31 00:02:24,300 --> 00:02:27,270 And maybe another break statement here. 32 00:02:29,870 --> 00:02:33,500 And then maybe inside of here, I'm also going to have a print. 33 00:02:35,240 --> 00:02:36,050 Can you? 34 00:02:40,580 --> 00:02:42,620 What happens if I do something like this? 35 00:02:43,950 --> 00:02:45,570 Pause the video if you need to. 36 00:02:45,810 --> 00:02:51,360 Otherwise, I'm going to hit, run and see what's going to happen, what is going to happen if I enter 37 00:02:51,390 --> 00:02:51,780 zero? 38 00:02:55,500 --> 00:03:02,880 I get please enter higher than zero because I get a zero division error, it catches here and then we 39 00:03:02,880 --> 00:03:10,830 break out of the loop, the finally gets run, but print never runs because, well, we break out of 40 00:03:10,830 --> 00:03:11,250 the loop. 41 00:03:11,250 --> 00:03:13,950 And remember, this is surrounded by the loop. 42 00:03:14,500 --> 00:03:16,260 Let me remove this so we can see it better. 43 00:03:18,280 --> 00:03:22,150 OK, what happens if I do something like this? 44 00:03:22,420 --> 00:03:26,920 What if we run the program again and this time give it jibberish? 45 00:03:31,400 --> 00:03:33,410 I get please enter your number. 46 00:03:33,770 --> 00:03:36,160 OK, I'm finally done, and then what's your age? 47 00:03:37,860 --> 00:03:38,870 So what happened here? 48 00:03:40,340 --> 00:03:46,790 Well, we tried this, we got an air, a value air that says, hey, please enter our number and then 49 00:03:46,790 --> 00:03:47,940 it's going to continue. 50 00:03:48,770 --> 00:03:50,540 Now continue instead of break. 51 00:03:50,780 --> 00:03:53,620 Doesn't break out of the loop, out of the while loop. 52 00:03:53,780 --> 00:03:56,060 Instead, it comes back to the top. 53 00:03:56,390 --> 00:03:58,070 So it's going to try again. 54 00:03:58,250 --> 00:03:59,540 This part, what's your age? 55 00:04:00,470 --> 00:04:06,220 OK, so what if we finally try to log in properly and give it, hey, we're 18. 56 00:04:06,920 --> 00:04:07,670 What happens in that? 57 00:04:09,190 --> 00:04:11,050 There you go, I get. 58 00:04:12,430 --> 00:04:18,340 Thank you, because we've tried it, everything works, so no exception, so we are going to get thank 59 00:04:18,340 --> 00:04:21,400 you and then we break out of the loop. 60 00:04:21,580 --> 00:04:25,080 So once again, this print never gets printed. 61 00:04:25,090 --> 00:04:25,690 Can you hear me? 62 00:04:25,990 --> 00:04:28,390 But then finally, OK, finally is done. 63 00:04:28,870 --> 00:04:33,460 If we didn't have this brake statement here and I click run. 64 00:04:34,790 --> 00:04:42,380 And give it 18 oh, boy, finally, I can hear you, this print finally gets printed because we don't 65 00:04:42,380 --> 00:04:47,390 break out of here, we don't break out of here, and then we run this line. 66 00:04:48,440 --> 00:04:50,160 Hopefully, that makes sense. 67 00:04:50,840 --> 00:04:54,410 That was a bit of a trick question, but you're starting to get the hang of it. 68 00:04:55,190 --> 00:04:56,540 I'll see you in the next video. 69 00:04:57,230 --> 00:04:57,550 Bye bye.