1 00:00:00,270 --> 00:00:03,070 Instructor: Let's go back to our example of our game 2 00:00:04,050 --> 00:00:06,780 where we have a user login 3 00:00:06,780 --> 00:00:08,550 and they have to provide their age. 4 00:00:08,550 --> 00:00:10,170 Remember, if I click "run," 5 00:00:10,170 --> 00:00:12,480 it's going to ask me, "Hey, what's your age?" 6 00:00:12,480 --> 00:00:15,000 And if I give it something that it doesn't like, 7 00:00:15,000 --> 00:00:16,290 it's going to keep asking me 8 00:00:16,290 --> 00:00:19,773 until I provide a proper value. 9 00:00:20,820 --> 00:00:23,940 There's one other piece to the try/except lock 10 00:00:23,940 --> 00:00:25,230 that we haven't covered. 11 00:00:25,230 --> 00:00:26,997 So we know that we have "try," 12 00:00:26,997 --> 00:00:29,190 we have "except," 13 00:00:29,190 --> 00:00:30,810 and then we have "else." 14 00:00:30,810 --> 00:00:34,290 But there's also another thing called "finally," 15 00:00:34,290 --> 00:00:37,740 and finally runs at the end 16 00:00:37,740 --> 00:00:40,039 after everything has been executed. 17 00:00:40,039 --> 00:00:41,250 Let me show you. 18 00:00:41,250 --> 00:00:46,137 If I do print, let's say, "Okay, I'm finally done." 19 00:00:50,100 --> 00:00:51,360 Let's see what happens. 20 00:00:51,360 --> 00:00:55,150 If I click run here and I give it a proper age 21 00:00:56,310 --> 00:01:00,900 I get "thank you" because my try block succeeded. 22 00:01:00,900 --> 00:01:02,460 Nothing has failed. 23 00:01:02,460 --> 00:01:05,637 I go to the else and I say "thank you." 24 00:01:06,930 --> 00:01:11,670 And now I break out of the while loop, 25 00:01:11,670 --> 00:01:13,027 but finally says, 26 00:01:13,027 --> 00:01:15,210 "Hey, no matter what, 27 00:01:15,210 --> 00:01:18,867 at the end of it all, I want you to finally do something." 28 00:01:19,920 --> 00:01:20,850 Let's see this in action 29 00:01:20,850 --> 00:01:22,713 if I do something like zero. 30 00:01:23,910 --> 00:01:27,150 I get, "Please enter age higher than zero." 31 00:01:27,150 --> 00:01:30,420 But then also still get, "Okay, I'm finally done." 32 00:01:30,420 --> 00:01:31,983 What if I enter gibberish? 33 00:01:33,030 --> 00:01:35,550 I get, "Hey, please enter a number," 34 00:01:35,550 --> 00:01:38,490 but also, "Okay, I'm finally done." 35 00:01:38,490 --> 00:01:43,490 So finally runs regardless at the end of everything. 36 00:01:45,150 --> 00:01:47,400 Why is this useful? 37 00:01:47,400 --> 00:01:49,530 Maybe you're working on a game server 38 00:01:49,530 --> 00:01:52,170 and you wanna make sure that you log out 39 00:01:52,170 --> 00:01:54,300 any activity on the server. 40 00:01:54,300 --> 00:01:57,480 So, let's say a user tries to log in. 41 00:01:57,480 --> 00:02:00,630 Even if they try to enter the wrong information, 42 00:02:00,630 --> 00:02:02,640 we want to log that activity, 43 00:02:02,640 --> 00:02:04,320 so that maybe we can detect 44 00:02:04,320 --> 00:02:06,690 that there's people trying to break our program, 45 00:02:06,690 --> 00:02:08,407 or just to have our records that, 46 00:02:08,407 --> 00:02:11,850 "Hey, this user has tried to log in 10 times." 47 00:02:11,850 --> 00:02:14,430 So finally is very useful. 48 00:02:14,430 --> 00:02:17,820 Now let me ask you a question. 49 00:02:17,820 --> 00:02:20,080 What happens if I do something like this? 50 00:02:20,080 --> 00:02:23,007 Let's say I have here "continue," 51 00:02:24,360 --> 00:02:27,423 and maybe another break statement here, 52 00:02:29,880 --> 00:02:31,890 and then maybe inside of here, 53 00:02:31,890 --> 00:02:33,663 I'm also going to have a print, 54 00:02:35,107 --> 00:02:37,827 "Can you hear me?" 55 00:02:40,590 --> 00:02:42,783 What happens if I do something like this? 56 00:02:43,950 --> 00:02:45,810 Pause the video if you need to. 57 00:02:45,810 --> 00:02:47,580 Otherwise, I'm going to hit "run," 58 00:02:47,580 --> 00:02:49,410 and see what's going to happen. 59 00:02:49,410 --> 00:02:52,710 What is going to happen if I enter zero? 60 00:02:52,710 --> 00:02:53,543 Ready? 61 00:02:55,530 --> 00:02:58,350 I get, "Please enter age higher than zero," 62 00:02:58,350 --> 00:03:01,200 because I get a ZeroDivisionError. 63 00:03:01,200 --> 00:03:05,040 It catches here, and then we break out of the loop. 64 00:03:05,040 --> 00:03:09,339 The finally gets run, but print never runs because, 65 00:03:09,339 --> 00:03:11,220 well, we break out of the loop. 66 00:03:11,220 --> 00:03:14,520 And remember, this is surrounded by the loop. 67 00:03:14,520 --> 00:03:16,670 Let me remove this so we can see it better. 68 00:03:18,300 --> 00:03:22,410 Okay, what happens if I do something like this? 69 00:03:22,410 --> 00:03:24,540 What if we run the program again 70 00:03:24,540 --> 00:03:27,063 and this time give it gibberish? 71 00:03:28,500 --> 00:03:29,333 Let's find out. 72 00:03:31,440 --> 00:03:35,220 I get, "Please enter a number. Okay, I'm finally done," 73 00:03:35,220 --> 00:03:36,620 and then, "What's your age?" 74 00:03:37,890 --> 00:03:39,033 So what happened here? 75 00:03:40,350 --> 00:03:42,060 Well, we tried this, 76 00:03:42,060 --> 00:03:43,980 we got an error, a value error. 77 00:03:43,980 --> 00:03:46,410 It says, "Hey, please enter a number," 78 00:03:46,410 --> 00:03:48,780 and then it's going to continue. 79 00:03:48,780 --> 00:03:50,730 Now continue instead of break 80 00:03:50,730 --> 00:03:53,820 doesn't break out of the loop, out of the while loop. 81 00:03:53,820 --> 00:03:56,400 Instead it comes back to the top. 82 00:03:56,400 --> 00:03:58,260 So it's going to try again. 83 00:03:58,260 --> 00:04:00,480 This part, "What's your age?" 84 00:04:00,480 --> 00:04:04,530 Okay, so what if we finally try to log in properly 85 00:04:04,530 --> 00:04:06,960 and give it, "Hey, we're 18." 86 00:04:06,960 --> 00:04:07,860 What happens then? 87 00:04:09,210 --> 00:04:10,530 There you go. 88 00:04:10,530 --> 00:04:13,200 I get, "Thank you," 89 00:04:13,200 --> 00:04:15,420 because we've tried it, everything works. 90 00:04:15,420 --> 00:04:17,010 So, no exception. 91 00:04:17,010 --> 00:04:19,050 So we are gonna get "thank you" 92 00:04:19,050 --> 00:04:21,600 and then we break out of the loop. 93 00:04:21,600 --> 00:04:24,967 So once again, this print never gets printed, 94 00:04:24,967 --> 00:04:26,040 "Can you hear me?" 95 00:04:26,040 --> 00:04:28,890 But then finally, "Okay, finally is done." 96 00:04:28,890 --> 00:04:31,827 If we didn't have this break statement here 97 00:04:31,827 --> 00:04:33,717 and I click "run," 98 00:04:34,860 --> 00:04:37,177 and give it "18," oh boy, 99 00:04:37,177 --> 00:04:39,360 "Finally I can hear you." 100 00:04:39,360 --> 00:04:41,340 This print finally gets printed 101 00:04:41,340 --> 00:04:43,860 because we don't break out of here. 102 00:04:43,860 --> 00:04:47,523 We don't break out of here, and then we run this line. 103 00:04:48,420 --> 00:04:50,910 Hopefully that makes sense. 104 00:04:50,910 --> 00:04:52,260 That was a bit of a trick question 105 00:04:52,260 --> 00:04:55,200 but you're starting to get the hang of it. 106 00:04:55,200 --> 00:04:56,935 I'll see you in the next video. 107 00:04:56,935 --> 00:04:57,768 Bye, bye.