Welcome back. So now let us add the function to allow our users to log in.
So I'm gonna say the fine and let's go
with the login uh underscore will be the username.
Let's add a colon. Actually, let's add a note. Ok? And we can just say login function
and there you go. All right.
So in order for the user to log in,
they're going to require the username and the password.
So
let us start by saying username equals and then input
in brackets codes, enter your username colon.
OK?
And then we can also add the password equals input brackets,
enter your password.
And there you go. All right.
Now, in order
to verify that the username and password do exist,
we're gonna have to open up our users dot
TXT file. So I'm gonna say with
open
and now in brackets, what's the name of the file? It is users dot
TXT.
And then what is the mode
that we're going with? We're going with the read mode.
OK? Because we want to read from the file
and I'm gonna say as file
colon.
And now let us create a verbal called users
that would accept the file dot Readlines function because remember
we're gonna have to read line by line from each user
in the users dot text file. So
I'll say users equals to file
dot Readlines and now
brackets and there it is. So now we need to check if the
credentials match any stored record.
So what are we gonna do? We're gonna create our for loop.
So I'm gonna say four,
let us use the variable user in users.
OK?
So now
what do we want to check? We want to check for the user name and then the password.
So we can create two variables in here. We can go with this stored,
OK? Stored underscore
username
and then comma we can also go with stored
underscore password, right? A lot of creates two variables to represent
the store username and then the store password.
And now how would we check?
We need to first of all
check to see if the user name is correct
and then also the password. But now take a look at this. Ok.
The users dot CX file as it is we have Alex and then colon Alex. So
we need to figure out a way how we can separate the user name from the password.
And we know that it's the colon, the colon symbol that separates them.
So we can use the split function
to separate the user name and the password.
But we can also use the strip function remember the strip function,
we use that to remove any white characters or empty spaces, things like that. Ok.
So let's go back in here. So what we're gonna do right now is I'm going to say user
and now let us add the strip function
to remove all the necessary space from the username.
Ok.
Our brackets. And now I'm going to add the split function.
And what are we splitting the username and the password by is going to be the colon.
And there you go. So we're gonna split the username and the password by the colon.
So from here right now,
we can check,
we can say if
the username
is the same
as what the store
underscore username
and what and password
is equal as what is stored underscore
password.
What do we want to do?
We want to print
and say
log in
successful
and there you go.
Awesome. Awesome. Awesome. And then from here,
we can simply return for now. Ok, later on we will add
additional functionality. But for now, let's just return
and go back to the beginning of the program. So we're going to exit the function
after a successful login.
And of course, we can also print just in case something
did not go correctly.
We can print
nine brackets.
We can simply say invalid
username or
password.
And
there you go, let me just add
some space in here
and there we go. So hopefully this should work let me
integrate it into the main function. So what we want to do right now is
where it says the
choice number two,
we can remove the print statements and now simply add
the name of the function which is login under
user
and now brackets and there it is OK,
fingers crossed. Let us run the program
and let us see.
I'm gonna go straight to log in because
we've already created the Alex username and password.
So,
user name is Alex, password is Alex. And there you go. Awesome. Login successful.
Let us try to see if we'll accept
a different username that doesn't exist. So
I'm gonna go with login and let's go with Philip
enter password, sword fish.
And there we go, it says invalid username or password. So it is working. Awesome.
Awesome. Awesome.
So we've successfully added the registration function.
We've successfully added the login function as well.
So now join me in the very next video where
we're going to advance the program by introducing the password checker
that will ensure that the password created meets a certain strength criteria.
I'll see you then.