1 00:00:00,009 --> 00:00:07,670 Welcome back. So I have decided to add one more task to make it the third task 2 00:00:08,079 --> 00:00:10,210 and it's going to be a bit more complicated. 3 00:00:10,539 --> 00:00:12,649 And this task is going to involve counting 4 00:00:12,659 --> 00:00:16,549 the number of failed login attempts per user. 5 00:00:16,879 --> 00:00:19,489 So essentially we're going to go through each log, 6 00:00:19,500 --> 00:00:22,940 count the number of times a particular user 7 00:00:22,950 --> 00:00:25,760 failed in their status and then simply print out 8 00:00:26,270 --> 00:00:28,709 Alice has, you know, four failures. 9 00:00:28,719 --> 00:00:31,659 Bob has one failure and so on, you know, something like that. So 10 00:00:32,139 --> 00:00:34,080 you're more than welcome to pause the video and 11 00:00:34,090 --> 00:00:37,020 attempt to solve this task on your own. 12 00:00:37,029 --> 00:00:37,930 If not, 13 00:00:38,200 --> 00:00:40,680 let's go through this together. So 14 00:00:40,819 --> 00:00:43,659 like I said earlier, I have my notepad file in here because 15 00:00:44,110 --> 00:00:45,900 I want us to tackle this, 16 00:00:46,180 --> 00:00:50,029 tackle this logically. OK. So let's go step by step. 17 00:00:50,040 --> 00:00:51,819 The very first question here is this, 18 00:00:52,709 --> 00:00:55,209 are we going to be dealing 19 00:00:55,419 --> 00:00:56,759 with assets, 20 00:00:57,490 --> 00:01:01,189 a dictionary or a list? Let's think about it, right? 21 00:01:01,900 --> 00:01:07,360 Obviously, it cannot be a set because sets do not accept duplicates. 22 00:01:07,760 --> 00:01:11,919 So if we're going to use a set in here, and Bob had 23 00:01:12,089 --> 00:01:13,919 one field login attempt, 24 00:01:14,129 --> 00:01:16,930 the second time Bob has a failed attempt, 25 00:01:17,139 --> 00:01:20,849 the set will not accept it because remember set do not accept duplicate. 26 00:01:20,860 --> 00:01:22,370 So sets will not work here. 27 00:01:22,730 --> 00:01:25,910 It cannot be a list either because think about 28 00:01:26,089 --> 00:01:26,110 it, 29 00:01:26,809 --> 00:01:34,059 we need to associate the user name with the number of times they have failed. 30 00:01:34,540 --> 00:01:40,480 And it's dictionaries that are great for working with key value pairs. 31 00:01:40,660 --> 00:01:43,330 So the key here will be the username 32 00:01:43,449 --> 00:01:45,519 and then the value is going to be the number 33 00:01:45,529 --> 00:01:50,190 of times that particular username has filled in their status. 34 00:01:50,199 --> 00:01:54,309 So we're going to be working with a dictionary. So we need 35 00:01:54,750 --> 00:01:55,930 dictionary 36 00:01:56,790 --> 00:01:57,919 to store 37 00:01:58,519 --> 00:02:02,319 username and their respective 38 00:02:03,260 --> 00:02:05,089 uh failed 39 00:02:05,519 --> 00:02:08,839 login attempts, right? OK. 40 00:02:09,160 --> 00:02:09,919 Now 41 00:02:10,369 --> 00:02:11,800 what do we need to do 42 00:02:12,020 --> 00:02:16,020 to actually find out the number of times each user has failed. 43 00:02:16,029 --> 00:02:18,960 We need to create a for loop. So 44 00:02:19,330 --> 00:02:21,229 for loop to go through 45 00:02:21,779 --> 00:02:23,720 each log, right? And then 46 00:02:23,949 --> 00:02:26,619 when a loop, when it goes through a log, 47 00:02:26,860 --> 00:02:31,139 what are we looking for? Exactly? We want to look for the status 48 00:02:31,289 --> 00:02:32,830 that says failure. 49 00:02:32,839 --> 00:02:36,270 We're not interested in the, in the success, we're interested in failure. 50 00:02:36,279 --> 00:02:37,520 So we can say if 51 00:02:37,750 --> 00:02:40,080 the status, so if status 52 00:02:40,979 --> 00:02:44,110 equals to failure, what do we want to do? 53 00:02:44,350 --> 00:02:47,529 We want to identify the username 54 00:02:47,639 --> 00:02:50,169 associated with that failure. 55 00:02:50,320 --> 00:02:52,789 So let us extract 56 00:02:53,009 --> 00:02:57,470 the user name. OK, we're going to extract the username and then we can store that in 57 00:02:57,919 --> 00:03:00,149 a variable called let's say username, right. 58 00:03:00,419 --> 00:03:03,009 So we can store the usernames in the variable username. 59 00:03:03,119 --> 00:03:03,789 So 60 00:03:04,149 --> 00:03:09,229 once that username has been found, let me correct this one extract. 61 00:03:09,759 --> 00:03:10,710 OK? So 62 00:03:11,160 --> 00:03:15,710 once a user name has been found to have a status of failure, 63 00:03:15,929 --> 00:03:18,820 there are two possible scenarios here. Think about 64 00:03:18,979 --> 00:03:19,000 it. 65 00:03:19,570 --> 00:03:22,899 It's either this is the very first time 66 00:03:22,910 --> 00:03:26,699 that username has had a failed login attempt or 67 00:03:26,949 --> 00:03:27,899 it could be 68 00:03:28,399 --> 00:03:32,059 the second time or third time or fourth time. Essentially, 69 00:03:32,509 --> 00:03:34,500 it could be, it's not the first time. 70 00:03:35,190 --> 00:03:36,020 So 71 00:03:36,179 --> 00:03:39,139 we need to create conditions for both of them. So we can say 72 00:03:39,440 --> 00:03:42,059 if it's the first time, 73 00:03:42,559 --> 00:03:44,460 OK? We can say, all right, 74 00:03:44,750 --> 00:03:49,460 the username here that the score is zero because it's the first time. OK? 75 00:03:49,660 --> 00:03:50,699 However, 76 00:03:51,470 --> 00:03:53,160 if it's not the first time, 77 00:03:54,199 --> 00:03:55,550 if it's not the first time, 78 00:03:55,809 --> 00:03:57,460 we need to add 79 00:03:57,679 --> 00:03:58,580 one, 80 00:03:58,940 --> 00:03:59,589 OK? 81 00:03:59,809 --> 00:04:03,779 And then also add it to their previous record. 82 00:04:04,500 --> 00:04:08,600 So if it's the very first time you start from zero, you get one. 83 00:04:08,779 --> 00:04:12,020 But if it's not the first time, if it's the second time or third time, 84 00:04:12,029 --> 00:04:16,589 we're gonna simply add one to your previous record. 85 00:04:16,970 --> 00:04:19,298 I hope that makes sense. And then finally, 86 00:04:19,690 --> 00:04:21,858 we can simply print the failed 87 00:04:22,048 --> 00:04:24,040 login attempts. 88 00:04:25,619 --> 00:04:28,450 OK? So you can pause the video again 89 00:04:28,649 --> 00:04:32,019 and try to solve this task on your own. 90 00:04:32,839 --> 00:04:33,600 If not, 91 00:04:33,940 --> 00:04:37,209 let's write the program together. OK? So 92 00:04:38,200 --> 00:04:39,660 what are we doing? First of all, 93 00:04:40,160 --> 00:04:45,420 we are creating our dictionary. So I'm gonna call the dictionary field 94 00:04:46,299 --> 00:04:48,359 uh attempts. OK. 95 00:04:49,100 --> 00:04:53,950 Equals and then curly braces is going to be empty because nothing is in there for now. 96 00:04:54,279 --> 00:04:54,829 So 97 00:04:55,089 --> 00:05:00,369 next is gonna come the the four loops, I'm gonna say for log in logs, colon. 98 00:05:01,339 --> 00:05:03,220 And now what are we looking for? 99 00:05:03,619 --> 00:05:04,839 We're looking for, 100 00:05:06,100 --> 00:05:13,059 we're looking to check to see if the username has the status of failure. 101 00:05:13,070 --> 00:05:16,609 So specifically, we're looking to check if the status is a failure. So 102 00:05:16,959 --> 00:05:17,910 I'm gonna say 103 00:05:19,040 --> 00:05:20,250 uh if 104 00:05:21,320 --> 00:05:22,140 log 105 00:05:22,989 --> 00:05:27,600 and now we want to target the status, right? So if the status 106 00:05:27,989 --> 00:05:28,890 in here 107 00:05:29,369 --> 00:05:30,760 is equal, 108 00:05:31,750 --> 00:05:34,730 oh sorry about that, they should have been outside. 109 00:05:35,019 --> 00:05:36,679 So if status 110 00:05:36,779 --> 00:05:40,329 is equal to what you failure, 111 00:05:41,179 --> 00:05:42,450 capital F 112 00:05:42,730 --> 00:05:44,609 if it's equal to failure, 113 00:05:45,369 --> 00:05:51,750 what are we looking for? We want to extract the username. So I'm gonna say username 114 00:05:52,329 --> 00:05:54,329 will be equal to 115 00:05:54,730 --> 00:05:58,700 and now let's extract the name. I'm gonna say log 116 00:05:59,000 --> 00:06:00,429 and now in brackets 117 00:06:01,109 --> 00:06:02,269 user 118 00:06:02,489 --> 00:06:03,510 name, 119 00:06:04,250 --> 00:06:06,149 let me add the, the codes right there 120 00:06:06,820 --> 00:06:07,320 and 121 00:06:07,420 --> 00:06:09,320 there you go. OK. So 122 00:06:09,440 --> 00:06:13,359 what have we done so far? First of all, we've created 123 00:06:13,769 --> 00:06:14,480 the 124 00:06:14,809 --> 00:06:16,720 dictionary field attempts. 125 00:06:17,079 --> 00:06:19,440 Now we've created our for loop 126 00:06:19,730 --> 00:06:22,779 to go through each log in the logs 127 00:06:23,040 --> 00:06:27,410 and then look for any log whose status is equal to failure. 128 00:06:27,529 --> 00:06:29,839 When you find that status, 129 00:06:29,989 --> 00:06:31,190 that's equal to failure. 130 00:06:31,200 --> 00:06:35,880 Let us extract the username and then assign it to the variable user name. 131 00:06:36,250 --> 00:06:38,709 Now, remember there's gonna be two conditions. 132 00:06:38,839 --> 00:06:40,549 Is this the first time 133 00:06:40,760 --> 00:06:41,950 this username has this as a 134 00:06:42,190 --> 00:06:43,700 failure or 135 00:06:43,850 --> 00:06:46,630 is it not the first time. So 136 00:06:46,750 --> 00:06:49,019 check this out. I'm gonna say 137 00:06:49,720 --> 00:06:50,660 uh if 138 00:06:52,510 --> 00:06:53,529 username, 139 00:06:54,720 --> 00:06:59,609 OK, is not, how do we check if it's the first time if it's not in 140 00:07:00,019 --> 00:07:02,179 the field attempts? 141 00:07:02,429 --> 00:07:04,820 So by saying if this is the first time 142 00:07:05,239 --> 00:07:09,600 that we're actually discovering that, that this user has a failed record, 143 00:07:09,809 --> 00:07:13,230 so we can check that by saying user name is not in the field attempts. 144 00:07:13,359 --> 00:07:15,579 What do we want to do? We want to say failed 145 00:07:17,200 --> 00:07:17,920 attempts 146 00:07:20,089 --> 00:07:21,559 and now in brackets 147 00:07:23,369 --> 00:07:24,350 user name, 148 00:07:26,540 --> 00:07:29,709 it's gonna be equal to zero. 149 00:07:31,100 --> 00:07:31,899 However, 150 00:07:32,959 --> 00:07:36,600 if it's oh, sorry about that, let me field attempts 151 00:07:38,500 --> 00:07:43,320 attempt. Actually, I do apologize. I've made a mistake with the attempts 152 00:07:43,529 --> 00:07:44,579 that she made tea 153 00:07:45,410 --> 00:07:46,299 for the S 154 00:07:47,869 --> 00:07:48,709 uh 155 00:07:49,660 --> 00:07:52,619 I do apologize. Let me add my tea there 156 00:07:53,059 --> 00:07:54,850 and add my tea there. OK? 157 00:07:54,989 --> 00:08:00,239 So, however, if it's not the first time, if it's the second time, third time, 158 00:08:00,250 --> 00:08:00,989 what are we doing? 159 00:08:01,190 --> 00:08:02,619 We are 160 00:08:03,000 --> 00:08:04,679 saying failed 161 00:08:06,269 --> 00:08:07,160 attempt 162 00:08:08,869 --> 00:08:10,649 and now username, 163 00:08:13,250 --> 00:08:14,269 username 164 00:08:14,730 --> 00:08:15,679 is going to be 165 00:08:16,769 --> 00:08:17,480 equal 166 00:08:20,980 --> 00:08:23,079 plus equal to one 167 00:08:24,049 --> 00:08:26,579 because we're gonna be incrementing the value. 168 00:08:26,589 --> 00:08:29,130 So if there was one before now it's gonna become two. 169 00:08:29,140 --> 00:08:32,630 If it was two before now, it's gonna become three and so on. 170 00:08:32,909 --> 00:08:37,950 I want to point out the fact that notice the second failed attempts here, 171 00:08:38,159 --> 00:08:42,989 it's outside, it's not inside this if statement. OK? 172 00:08:43,159 --> 00:08:45,729 Because this right here lines 2122 is just to check 173 00:08:45,739 --> 00:08:49,159 to see if the username did not exist previously. 174 00:08:49,469 --> 00:08:52,890 And now the second field attempts here, this is a check to see if 175 00:08:53,429 --> 00:08:54,219 indeed 176 00:08:54,500 --> 00:08:56,489 the failed attempt has already been recorded 177 00:08:56,500 --> 00:08:59,890 previously and now we're incrementing by one. 178 00:08:59,900 --> 00:09:01,450 So all we need to do right now 179 00:09:01,909 --> 00:09:03,049 is to print. 180 00:09:04,219 --> 00:09:05,599 Ok. And then make sure this is 181 00:09:06,440 --> 00:09:07,239 outside 182 00:09:08,609 --> 00:09:11,669 prints. And now in brackets, we can say 183 00:09:12,679 --> 00:09:14,789 something like filled 184 00:09:15,140 --> 00:09:16,330 uh, 185 00:09:18,359 --> 00:09:20,820 field login 186 00:09:22,390 --> 00:09:23,270 attempts 187 00:09:25,479 --> 00:09:28,099 per user and then colon 188 00:09:28,330 --> 00:09:31,390 and then we can simply now addfield 189 00:09:31,729 --> 00:09:32,619 underscore 190 00:09:32,750 --> 00:09:33,659 attempts 191 00:09:35,590 --> 00:09:37,549 and there you go. OK. So hopefully 192 00:09:37,750 --> 00:09:44,609 this should work. I'm gonna run the program and there you go. Awesome. So, 193 00:09:45,020 --> 00:09:48,710 Bob was three, Alice was two and Dave was one. 194 00:09:49,159 --> 00:09:54,770 I think this is correct. Bob has one here. 11, that's three. OK. Alice has 195 00:09:55,179 --> 00:09:58,469 Alice was, was successful here. OK? There's a failure, fail two. OK? 196 00:09:58,849 --> 00:10:04,150 And then Dave is one and of course, Carol did not show because Carol had only one log 197 00:10:04,330 --> 00:10:06,750 and she was successful. So 198 00:10:07,359 --> 00:10:11,289 that's pretty much it on solving this particular task. 199 00:10:11,570 --> 00:10:13,179 So once again, 200 00:10:13,340 --> 00:10:15,190 we created a dictionary 201 00:10:15,440 --> 00:10:16,750 called field attempts. 202 00:10:16,919 --> 00:10:20,770 We created a loop to go through each log and then say, OK, 203 00:10:20,780 --> 00:10:23,640 if we do find a status that's failure, we 204 00:10:23,750 --> 00:10:24,630 wanna check, 205 00:10:24,640 --> 00:10:28,570 we first of all wanna extract the username associated with that failure 206 00:10:28,820 --> 00:10:34,549 and then check to see if the username has been in our field attempts previously. 207 00:10:34,559 --> 00:10:39,320 Or if, if it's the first time, if it's the first time I I send them the value of zero. 208 00:10:39,750 --> 00:10:46,190 And then if it's not the first time, simply add one to the previous value, 209 00:10:46,380 --> 00:10:52,669 by the way, a confusion you might be having here is, wait, hold on, Alex. Why is it 210 00:10:53,270 --> 00:10:57,510 that here? Line 2122. If it's the first time 211 00:10:58,020 --> 00:10:58,679 that 212 00:10:59,210 --> 00:11:03,179 their record is a failure that the username has a record of failure. 213 00:11:03,190 --> 00:11:06,030 Why not say equals to one 214 00:11:06,309 --> 00:11:07,890 yz 215 00:11:08,020 --> 00:11:08,659 low? 216 00:11:09,169 --> 00:11:10,549 The reason is because 217 00:11:10,989 --> 00:11:12,650 line 23 218 00:11:12,830 --> 00:11:13,750 we 219 00:11:13,979 --> 00:11:17,070 regardless are going to add one 220 00:11:17,820 --> 00:11:22,359 to the value whether or not it's the first time or it's the second time. 221 00:11:22,369 --> 00:11:24,239 So it doesn't even matter. 222 00:11:24,630 --> 00:11:26,479 I hope you get what I'm saying, right? Because 223 00:11:26,799 --> 00:11:30,710 this is gonna go. So if it's, if it's the very first time, right? First of all, 224 00:11:30,840 --> 00:11:35,619 the program will say, OK, your fieldd number of terms is zero on line 22 225 00:11:35,770 --> 00:11:40,719 and then on line 23 it's going to add one regardless. 226 00:11:41,250 --> 00:11:46,719 That's why we are assigned zero in here and not one. If we assigned one here, 227 00:11:46,960 --> 00:11:48,390 then automatically 228 00:11:49,349 --> 00:11:53,640 whoever had just one failed attempt will now have two 229 00:11:53,650 --> 00:11:56,590 against their record and to prove it to you, 230 00:11:56,690 --> 00:12:00,799 you can see right now, Dave has just one failed login attempt, right? 231 00:12:00,900 --> 00:12:02,549 If I came here right now 232 00:12:02,760 --> 00:12:03,289 and I 233 00:12:03,559 --> 00:12:05,369 change 0 to 1 instead 234 00:12:05,719 --> 00:12:10,330 and I run the program. Look at that, Dave now has two. 235 00:12:11,159 --> 00:12:14,929 That's why we made this 10 1st 236 00:12:15,049 --> 00:12:18,299 because we know that on line 23 on the very next step, 237 00:12:18,320 --> 00:12:22,270 one will be added to that user record regardless of whether 238 00:12:22,280 --> 00:12:24,650 or not it is the first time or their second time. 239 00:12:24,859 --> 00:12:27,690 So hopefully, you understand what we did there. 240 00:12:27,919 --> 00:12:31,210 Thank you for watching and of course, I will see you in the next class.