1 00:00:01,240 --> 00:00:04,900 In programming, a while loop is code that continuously repeats 2 00:00:04,900 --> 00:00:07,690 until a certain condition is no longer met. 3 00:00:07,690 --> 00:00:10,760 If I think about my day today, I've already used a few of these loops. 4 00:00:12,290 --> 00:00:16,360 In the shower this morning, while there was soap on my body, I rinsed. 5 00:00:16,360 --> 00:00:19,800 Afterwards, while I was wet, I dried my body. 6 00:00:19,800 --> 00:00:22,040 There's two loops and I was just waking up. 7 00:00:22,040 --> 00:00:27,230 Then while my coffee cup was empty, I filled it, once my cup was full, 8 00:00:27,230 --> 00:00:32,060 I stopped pouring, while there was cereal left in my bowl, I took some bites. 9 00:00:32,060 --> 00:00:35,220 Loops occur all the time in all applications that we build, 10 00:00:35,220 --> 00:00:38,610 while data is loading, show that waiting spinner. 11 00:00:38,610 --> 00:00:43,630 While the deal that will expire eventually hasn't yet, update the countdown clock. 12 00:00:43,630 --> 00:00:46,940 While the user hasn't entered the correct password, prompt for a retry. 13 00:00:48,240 --> 00:00:49,310 That's a great idea. 14 00:00:49,310 --> 00:00:51,490 Let's write a simple little password checker loop. 15 00:00:53,180 --> 00:00:59,850 So this is not going to be very robust and you should never actually use this code 16 00:00:59,850 --> 00:01:04,560 in a final application but it's a pretty good example of how while loops work. 17 00:01:04,560 --> 00:01:06,479 So I'm just giving you this warning now. 18 00:01:06,479 --> 00:01:10,662 This is like one of those do not try this at home warnings, okay? 19 00:01:10,662 --> 00:01:17,030 Right, so let's do this, let's create a new file, we'll say File, 20 00:01:17,030 --> 00:01:22,247 New File, and we will call this password_checker .py. 21 00:01:25,176 --> 00:01:29,073 So what we'll do right from the start is we'll prompt for a password. 22 00:01:29,073 --> 00:01:33,547 It'll say password = input. 23 00:01:33,547 --> 00:01:41,765 Please enter a super secret password. 24 00:01:41,765 --> 00:01:44,675 This is not a, it's the. 25 00:01:44,675 --> 00:01:48,465 Please enter the super secret password. 26 00:01:49,565 --> 00:01:53,730 Gave some space there, now already that's a bad idea. 27 00:01:53,730 --> 00:01:56,000 So here this password is gonna be right on the screen. 28 00:01:56,000 --> 00:01:56,810 Right as you type it out. 29 00:01:56,810 --> 00:01:58,860 Someone could look over their shoulder and capture it. 30 00:01:58,860 --> 00:02:01,050 Do not try this at home. 31 00:02:01,050 --> 00:02:01,790 Okay. 32 00:02:01,790 --> 00:02:06,980 So if the user doesn't get the password right, we should let them try again. 33 00:02:06,980 --> 00:02:09,130 Password typos are super common. 34 00:02:09,130 --> 00:02:13,530 So what we'll do is we'll use a new keyword that kicks off our loop. 35 00:02:13,530 --> 00:02:18,320 And that word is while and that's followed by an expression that will be 36 00:02:18,320 --> 00:02:21,080 checked each iteration through the loop. 37 00:02:21,080 --> 00:02:26,020 This is very much like an if statement, so if the expression is true, 38 00:02:26,020 --> 00:02:27,790 continue the loop. 39 00:02:27,790 --> 00:02:34,730 So that expression is if password is not equal to whatever the password is. 40 00:02:34,730 --> 00:02:38,910 So we'll say opensesame, that's my password by the way. 41 00:02:40,210 --> 00:02:43,270 So we've got a colon, so we're into a block of code that will repeat. 42 00:02:44,520 --> 00:02:45,732 And that's what we'll do is we'll just ask again. 43 00:02:45,732 --> 00:02:50,052 And we'll say password = input 44 00:02:50,052 --> 00:02:55,972 ("Invalid password, try again: "). 45 00:02:55,972 --> 00:02:59,707 And what happens is after the last line of the while block is finished, 46 00:02:59,707 --> 00:03:02,998 there's only one right now, so after this line is finished, 47 00:03:02,998 --> 00:03:05,748 execution returns to the while expression again. 48 00:03:05,748 --> 00:03:10,660 And if this is true, it will run the block again. 49 00:03:10,660 --> 00:03:14,532 So let's go ahead and write a message so that we can see if we were successful, 50 00:03:14,532 --> 00:03:18,120 let's see, let them into this little secret world we have over here. 51 00:03:18,120 --> 00:03:20,300 So welcome to secret town. 52 00:03:21,690 --> 00:03:25,237 Okay, let's give this a run, so 53 00:03:25,237 --> 00:03:29,560 we'll say python password_checker. 54 00:03:31,897 --> 00:03:35,470 Enter the super secret password. 55 00:03:36,590 --> 00:03:42,264 So I'm gonna enter in food, invalid password, please try again. 56 00:03:42,264 --> 00:03:45,916 So because password, which was food, 57 00:03:45,916 --> 00:03:50,564 did not equal open sesame, this line of code and so 58 00:03:50,564 --> 00:03:55,892 we're here now, invalid password, please try again. 59 00:03:55,892 --> 00:03:57,370 Again, let's print it out to the screen. 60 00:03:57,370 --> 00:03:59,810 Let's do one more, the password to my luggage. 61 00:03:59,810 --> 00:04:04,490 And you'll see that, again, 12345 is not equal to opensesame. 62 00:04:04,490 --> 00:04:08,840 And it will continue until this condition is false. 63 00:04:08,840 --> 00:04:13,980 So let's make that false, opensesame is actually equal 64 00:04:13,980 --> 00:04:16,665 to opensesame, welcome to secret town. 65 00:04:18,110 --> 00:04:22,888 Again, this is totally not secure at all, anyone could actually just read this file 66 00:04:22,888 --> 00:04:26,810 here and see our password in plain text right there, not a good idea. 67 00:04:26,810 --> 00:04:29,402 Another thing to take note of is this, 68 00:04:29,402 --> 00:04:33,371 we'll never see that retry message if this is ever false, 69 00:04:33,371 --> 00:04:38,251 the loop would never run because the condition is false from the get go. 70 00:04:38,251 --> 00:04:39,590 You know what? 71 00:04:39,590 --> 00:04:43,080 Let's add an additional check, we don't want hackers to keep on trying and 72 00:04:43,080 --> 00:04:45,560 using brute force to figure out our password. 73 00:04:45,560 --> 00:04:49,500 Let's only allow them three attempts before we break out of it. 74 00:04:49,500 --> 00:04:53,140 So we'll keep track of our attempt count. 75 00:04:53,140 --> 00:04:59,140 So this one here, we'll say attempt_count = 1. 76 00:04:59,140 --> 00:05:04,190 And then we'll increment by one after each password attempt. 77 00:05:04,190 --> 00:05:09,220 So we'll say, here, we'll say attempt_count. 78 00:05:09,220 --> 00:05:11,744 And much like the in-place addition that we did on strings, 79 00:05:11,744 --> 00:05:13,037 you can do that on numbers too. 80 00:05:13,037 --> 00:05:15,669 So attempt_count += 1. 81 00:05:15,669 --> 00:05:19,460 So that's attempt_count = attempt_count +1, more or less, right? 82 00:05:19,460 --> 00:05:21,280 Just some nice short hand. 83 00:05:21,280 --> 00:05:26,246 So at the start of this loop, we can check and see if the count is more than three, 84 00:05:26,246 --> 00:05:27,002 all right? 85 00:05:27,002 --> 00:05:31,082 So we can say if attempt_count > 3. 86 00:05:33,286 --> 00:05:34,820 What should we do? 87 00:05:34,820 --> 00:05:40,830 So you can actually stop a program running but first we need to import a module and 88 00:05:40,830 --> 00:05:45,200 that module name is sys, which is not for sister, it's short for system. 89 00:05:46,860 --> 00:05:48,500 Import sys. 90 00:05:48,500 --> 00:05:51,930 And the name of the function that we're gonna call is exit. 91 00:05:51,930 --> 00:05:56,376 So we'll do sys.exit. 92 00:05:56,376 --> 00:05:58,911 Now the way that sys.exit works, 93 00:05:58,911 --> 00:06:04,020 if you pass any value to sys.exit, it's considered an error. 94 00:06:04,020 --> 00:06:07,810 So whoever ran the program will get back the fact that an error happened in their 95 00:06:07,810 --> 00:06:10,220 code, which is kind of what we want to have happened here. 96 00:06:10,220 --> 00:06:12,240 So let's give it a message here. 97 00:06:12,240 --> 00:06:16,880 We'll say too many invalid password attempts. 98 00:06:18,260 --> 00:06:22,150 So let's clear this, see what happens. 99 00:06:23,290 --> 00:06:26,215 So we will run python password_checker. 100 00:06:29,180 --> 00:06:31,410 Okay, so let's try spam. 101 00:06:31,410 --> 00:06:32,328 Well, that didn't work. 102 00:06:32,328 --> 00:06:33,777 Let's try lumberjack. 103 00:06:33,777 --> 00:06:35,165 No? 104 00:06:35,165 --> 00:06:36,140 Cheeseshop? 105 00:06:37,870 --> 00:06:39,030 How about hovercraft? 106 00:06:40,030 --> 00:06:42,070 So this should be our last attempt, right? 107 00:06:43,170 --> 00:06:44,830 Too many invalid password attempts. 108 00:06:44,830 --> 00:06:47,640 And see, it printed it out there, too, really nice. 109 00:06:47,640 --> 00:06:50,120 Were those password choices confusing? 110 00:06:50,120 --> 00:06:53,570 One thing that I always like to make sure that people know when they're just getting 111 00:06:53,570 --> 00:06:58,120 started learning Python is that the language is not based on the snake. 112 00:06:58,120 --> 00:07:00,270 It is quite possibly, 113 00:07:00,270 --> 00:07:04,930 surprisingly, named after the British comedy group Monty Python. 114 00:07:05,940 --> 00:07:09,720 Now, I like to mention this because you're gonna see Python code in documentation and 115 00:07:09,720 --> 00:07:13,000 blog posts that use some very strange examples. 116 00:07:13,000 --> 00:07:15,760 You'll see lots of spam and eggs, and lumberjacks, and 117 00:07:15,760 --> 00:07:19,280 hovercrafts full of eels which is super weird. 118 00:07:19,280 --> 00:07:23,990 Especially if you don't know that all of these are based on Monty Python skits. 119 00:07:23,990 --> 00:07:27,170 So actually, I do recommend watching some Monty Python. 120 00:07:27,170 --> 00:07:31,237 Now not only because they are some of the most amazing absurdist comedy sketches 121 00:07:31,237 --> 00:07:35,300 ever created but also because it will help with some of the more inside jokes. 122 00:07:35,300 --> 00:07:37,120 It'll help make sense. 123 00:07:37,120 --> 00:07:41,640 I always feel for those of you who might be perplexed by the references. 124 00:07:41,640 --> 00:07:44,900 But also I can not even imagine what that must be like for 125 00:07:44,900 --> 00:07:48,170 learners who have English as a second or third language. 126 00:07:48,170 --> 00:07:50,680 A parrot is a speaking bird, no? 127 00:07:50,680 --> 00:07:54,090 Why is the code talking about a parrot being electrocuted? 128 00:07:54,090 --> 00:07:55,410 Is it because it speaks? 129 00:07:56,520 --> 00:08:01,040 No, I'm sorry, it's because of a joke that was made in the late 1970s. 130 00:08:01,040 --> 00:08:02,630 How did you miss that one? 131 00:08:02,630 --> 00:08:04,100 Check the teacher's notes for more. 132 00:08:04,100 --> 00:08:07,300 One more thing that I'd like to tackle stylistically here 133 00:08:07,300 --> 00:08:10,480 is that I don't like how our password is in the middle of this code, 134 00:08:10,480 --> 00:08:12,420 it's kind of down here in this code, right? 135 00:08:13,470 --> 00:08:16,170 It's a value that we might want to change but 136 00:08:16,170 --> 00:08:19,790 it will remain constant during the running of this program. 137 00:08:19,790 --> 00:08:24,570 So one thing that we can do is to create a variable near the top of the file and 138 00:08:24,570 --> 00:08:28,975 I'm gonna call it, underneath our import here, I'm gonna call it MASTER_PASSWORD. 139 00:08:30,850 --> 00:08:33,442 Now note that I used all capital letters. 140 00:08:33,442 --> 00:08:35,593 This is a naming convention for constants. 141 00:08:35,593 --> 00:08:40,441 So we'll say MASTER_PASSWORD and I'm gonna get rid of this here, put that here. 142 00:08:40,441 --> 00:08:43,190 And then I'm gonna use MASTER_PASSWORD here. 143 00:08:46,269 --> 00:08:51,045 This MASTER_PASSWORD is not something that I ever plan on changing while 144 00:08:51,045 --> 00:08:52,800 the program is running. 145 00:08:52,800 --> 00:08:54,400 It's a constant value. 146 00:08:54,400 --> 00:08:58,080 And I'm conveying that to readers of this code by using all capital letters and 147 00:08:58,080 --> 00:09:01,369 placing it at the top of the file so it's the first thing that they see. 148 00:09:02,660 --> 00:09:05,430 And then I'm gonna use that in our while loop. 149 00:09:05,430 --> 00:09:07,880 Now if we ever wanted to change the password, 150 00:09:07,880 --> 00:09:10,340 you just need to tweak the constant variable. 151 00:09:10,340 --> 00:09:12,560 Check the teacher's notes for more on constants. 152 00:09:12,560 --> 00:09:13,900 While loops are great for 153 00:09:13,900 --> 00:09:18,480 when you want code to run until a condition is no longer true. 154 00:09:18,480 --> 00:09:21,430 Now, in this case, you aren't sure when the loop will end. 155 00:09:22,480 --> 00:09:25,070 There's another type of loop that is great for 156 00:09:25,070 --> 00:09:27,720 when you have a certain amount of items to loop through. 157 00:09:27,720 --> 00:09:32,640 It's called for and I'll show you what it's good for right after this break.