Welcome back. So I have decided to add one more task to make it the third task
and it's going to be a bit more complicated.
And this task is going to involve counting
the number of failed login attempts per user.
So essentially we're going to go through each log,
count the number of times a particular user
failed in their status and then simply print out
Alice has, you know, four failures.
Bob has one failure and so on, you know, something like that. So
you're more than welcome to pause the video and
attempt to solve this task on your own.
If not,
let's go through this together. So
like I said earlier, I have my notepad file in here because
I want us to tackle this,
tackle this logically. OK. So let's go step by step.
The very first question here is this,
are we going to be dealing
with assets,
a dictionary or a list? Let's think about it, right?
Obviously, it cannot be a set because sets do not accept duplicates.
So if we're going to use a set in here, and Bob had
one field login attempt,
the second time Bob has a failed attempt,
the set will not accept it because remember set do not accept duplicate.
So sets will not work here.
It cannot be a list either because think about
it,
we need to associate the user name with the number of times they have failed.
And it's dictionaries that are great for working with key value pairs.
So the key here will be the username
and then the value is going to be the number
of times that particular username has filled in their status.
So we're going to be working with a dictionary. So we need
dictionary
to store
username and their respective
uh failed
login attempts, right? OK.
Now
what do we need to do
to actually find out the number of times each user has failed.
We need to create a for loop. So
for loop to go through
each log, right? And then
when a loop, when it goes through a log,
what are we looking for? Exactly? We want to look for the status
that says failure.
We're not interested in the, in the success, we're interested in failure.
So we can say if
the status, so if status
equals to failure, what do we want to do?
We want to identify the username
associated with that failure.
So let us extract
the user name. OK, we're going to extract the username and then we can store that in
a variable called let's say username, right.
So we can store the usernames in the variable username.
So
once that username has been found, let me correct this one extract.
OK? So
once a user name has been found to have a status of failure,
there are two possible scenarios here. Think about
it.
It's either this is the very first time
that username has had a failed login attempt or
it could be
the second time or third time or fourth time. Essentially,
it could be, it's not the first time.
So
we need to create conditions for both of them. So we can say
if it's the first time,
OK? We can say, all right,
the username here that the score is zero because it's the first time. OK?
However,
if it's not the first time,
we need to add
one,
OK?
And then also add it to their previous record.
So if it's the very first time you start from zero, you get one.
But if it's not the first time, if it's the second time or third time,
we're gonna simply add one to your previous record.
I hope that makes sense. And then finally,
we can simply print the failed
login attempts.
OK? So you can pause the video again
and try to solve this task on your own.
If not,
let's write the program together. OK? So
what are we doing? First of all,
we are creating our dictionary. So I'm gonna call the dictionary field
uh attempts. OK.
Equals and then curly braces is going to be empty because nothing is in there for now.
So
next is gonna come the the four loops, I'm gonna say for log in logs, colon.
And now what are we looking for?
We're looking for,
we're looking to check to see if the username has the status of failure.
So specifically, we're looking to check if the status is a failure. So
I'm gonna say
uh if
log
and now we want to target the status, right? So if the status
in here
is equal,
oh sorry about that, they should have been outside.
So if status
is equal to what you failure,
capital F
if it's equal to failure,
what are we looking for? We want to extract the username. So I'm gonna say username
will be equal to
and now let's extract the name. I'm gonna say log
and now in brackets
user
name,
let me add the, the codes right there
and
there you go. OK. So
what have we done so far? First of all, we've created
the
dictionary field attempts.
Now we've created our for loop
to go through each log in the logs
and then look for any log whose status is equal to failure.
When you find that status,
that's equal to failure.
Let us extract the username and then assign it to the variable user name.
Now, remember there's gonna be two conditions.
Is this the first time
this username has this as a
failure or
is it not the first time. So
check this out. I'm gonna say
uh if
username,
OK, is not, how do we check if it's the first time if it's not in
the field attempts?
So by saying if this is the first time
that we're actually discovering that, that this user has a failed record,
so we can check that by saying user name is not in the field attempts.
What do we want to do? We want to say failed
attempts
and now in brackets
user name,
it's gonna be equal to zero.
However,
if it's oh, sorry about that, let me field attempts
attempt. Actually, I do apologize. I've made a mistake with the attempts
that she made tea
for the S
uh
I do apologize. Let me add my tea there
and add my tea there. OK?
So, however, if it's not the first time, if it's the second time, third time,
what are we doing?
We are
saying failed
attempt
and now username,
username
is going to be
equal
plus equal to one
because we're gonna be incrementing the value.
So if there was one before now it's gonna become two.
If it was two before now, it's gonna become three and so on.
I want to point out the fact that notice the second failed attempts here,
it's outside, it's not inside this if statement. OK?
Because this right here lines 2122 is just to check
to see if the username did not exist previously.
And now the second field attempts here, this is a check to see if
indeed
the failed attempt has already been recorded
previously and now we're incrementing by one.
So all we need to do right now
is to print.
Ok. And then make sure this is
outside
prints. And now in brackets, we can say
something like filled
uh,
field login
attempts
per user and then colon
and then we can simply now addfield
underscore
attempts
and there you go. OK. So hopefully
this should work. I'm gonna run the program and there you go. Awesome. So,
Bob was three, Alice was two and Dave was one.
I think this is correct. Bob has one here. 11, that's three. OK. Alice has
Alice was, was successful here. OK? There's a failure, fail two. OK?
And then Dave is one and of course, Carol did not show because Carol had only one log
and she was successful. So
that's pretty much it on solving this particular task.
So once again,
we created a dictionary
called field attempts.
We created a loop to go through each log and then say, OK,
if we do find a status that's failure, we
wanna check,
we first of all wanna extract the username associated with that failure
and then check to see if the username has been in our field attempts previously.
Or if, if it's the first time, if it's the first time I I send them the value of zero.
And then if it's not the first time, simply add one to the previous value,
by the way, a confusion you might be having here is, wait, hold on, Alex. Why is it
that here? Line 2122. If it's the first time
that
their record is a failure that the username has a record of failure.
Why not say equals to one
yz
low?
The reason is because
line 23
we
regardless are going to add one
to the value whether or not it's the first time or it's the second time.
So it doesn't even matter.
I hope you get what I'm saying, right? Because
this is gonna go. So if it's, if it's the very first time, right? First of all,
the program will say, OK, your fieldd number of terms is zero on line 22
and then on line 23 it's going to add one regardless.
That's why we are assigned zero in here and not one. If we assigned one here,
then automatically
whoever had just one failed attempt will now have two
against their record and to prove it to you,
you can see right now, Dave has just one failed login attempt, right?
If I came here right now
and I
change 0 to 1 instead
and I run the program. Look at that, Dave now has two.
That's why we made this 10 1st
because we know that on line 23 on the very next step,
one will be added to that user record regardless of whether
or not it is the first time or their second time.
So hopefully, you understand what we did there.
Thank you for watching and of course, I will see you in the next class.